Home All Groups Group Topic Archive Search About

stringbuilder replace doesn't work on vbLf , vbCrLf , vbCr

Author
8 Apr 2005 12:49 PM
Guy
Hi,

I'm trying to run this code :

  strFileContentsHTML.Replace(vbLf, "<br>")
  strFileContentsHTML.Replace(vbCrLf, "<br>")
  strFileContentsHTML.Replace(vbCr, "<br>")

It doesn't replace newline characters with <br>.

Does anyone know how I should do this?

Thanks,
Guy

Author
8 Apr 2005 12:55 PM
Herfried K. Wagner [MVP]
"Guy" <G**@discussions.microsoft.com> schrieb:
> I'm trying to run this code :
>
>  strFileContentsHTML.Replace(vbLf, "<br>")
>  strFileContentsHTML.Replace(vbCrLf, "<br>")
>  strFileContentsHTML.Replace(vbCr, "<br>")
>
> It doesn't replace newline characters with <br>.

'StringBuilder.Replace' /returns/ a reference to a string builder which
contains the final data:

\\\
strFileContentsHtml = strFileContentsHtml.Replace(...)
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
8 Apr 2005 1:11 PM
Guy
Thanks for the repsonse Herfried.
I tried your suggestion and it still doesn work.

I changed the code to :

  strFileContentsHTML = strFileContentsHTML.Replace(vbLf, "<br>")
  strFileContentsHTML = strFileContentsHTML.Replace(vbCrLf, "<br>")
  strFileContentsHTML = strFileContentsHTML.Replace(vbCr, "<br>")

Earlier I tested

  strFileContentsHTML.Replace("a", "<br>")

and it worked fine. The problem seems to be only with the newline characters.

Guy



Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "Guy" <G**@discussions.microsoft.com> schrieb:
> > I'm trying to run this code :
> >
> >  strFileContentsHTML.Replace(vbLf, "<br>")
> >  strFileContentsHTML.Replace(vbCrLf, "<br>")
> >  strFileContentsHTML.Replace(vbCr, "<br>")
> >
> > It doesn't replace newline characters with <br>.
>
> 'StringBuilder.Replace' /returns/ a reference to a string builder which
> contains the final data:
>
> \\\
> strFileContentsHtml = strFileContentsHtml.Replace(...)
> ///
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>
>
Author
8 Apr 2005 1:28 PM
Cor Ligthert
Guy,

Strange, this test
\\\
Dim strFileContentsHTML As New System.Text.StringBuilder("Hello" & vbLf & _
   "how" & vbCrLf & "are" & vbCr & "you")
strFileContentsHTML = strFileContentsHTML.Replace(vbLf, "<br>")
strFileContentsHTML = strFileContentsHTML.Replace(vbCrLf, "<br>")
strFileContentsHTML = strFileContentsHTML.Replace(vbCr, "<br>")
Debug.WriteLine(strFileContentsHTML.ToString)
///
Has for me as result.
Hello<br>how<br><br>are<br>you

Be aware that the vbcrlf is of course already done first by the vblf and
will not be done in this way so that has actualy to be done first.

I hope this helps,

Cor
Author
8 Apr 2005 1:47 PM
Guy
Thanks for the response Cor.
That example works for me too.

I think I've found the problem.
I'm populating the string builder w/ :

InputStream.ReadLine

I assumed this returned everything upto and including endline characters but
apparently it only returns upto and not including the endline characters.

If I manually add my own "<br>" I get the result I'm looking for.

Guy


Show quoteHide quote
"Cor Ligthert" wrote:

> Guy,
>
> Strange, this test
> \\\
> Dim strFileContentsHTML As New System.Text.StringBuilder("Hello" & vbLf & _
>    "how" & vbCrLf & "are" & vbCr & "you")
> strFileContentsHTML = strFileContentsHTML.Replace(vbLf, "<br>")
> strFileContentsHTML = strFileContentsHTML.Replace(vbCrLf, "<br>")
> strFileContentsHTML = strFileContentsHTML.Replace(vbCr, "<br>")
> Debug.WriteLine(strFileContentsHTML.ToString)
> ///
> Has for me as result.
> Hello<br>how<br><br>are<br>you
>
> Be aware that the vbcrlf is of course already done first by the vblf and
> will not be done in this way so that has actualy to be done first.
>
> I hope this helps,
>
> Cor
>
>
>
Author
8 Apr 2005 1:30 PM
95isalive
Have you tried using the asci decimal value  &#10  or Chr(10) or Chr(13) instead of vbLf
and  vbCrLf


--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer
Show quoteHide quote
"Guy" <G**@discussions.microsoft.com> wrote in message
news:8F7FDEF6-062A-4475-BF59-6779A114F532@microsoft.com...
> Thanks for the repsonse Herfried.
> I tried your suggestion and it still doesn work.
>
> I changed the code to :
>
>   strFileContentsHTML = strFileContentsHTML.Replace(vbLf, "<br>")
>   strFileContentsHTML = strFileContentsHTML.Replace(vbCrLf, "<br>")
>   strFileContentsHTML = strFileContentsHTML.Replace(vbCr, "<br>")
>
> Earlier I tested
>
>   strFileContentsHTML.Replace("a", "<br>")
>
> and it worked fine. The problem seems to be only with the newline characters.
>
> Guy
>
>
>
> "Herfried K. Wagner [MVP]" wrote:
>
> > "Guy" <G**@discussions.microsoft.com> schrieb:
> > > I'm trying to run this code :
> > >
> > >  strFileContentsHTML.Replace(vbLf, "<br>")
> > >  strFileContentsHTML.Replace(vbCrLf, "<br>")
> > >  strFileContentsHTML.Replace(vbCr, "<br>")
> > >
> > > It doesn't replace newline characters with <br>.
> >
> > 'StringBuilder.Replace' /returns/ a reference to a string builder which
> > contains the final data:
> >
> > \\\
> > strFileContentsHtml = strFileContentsHtml.Replace(...)
> > ///
> >
> > --
> >  M S   Herfried K. Wagner
> > M V P  <URL:http://dotnet.mvps.org/>
> >  V B   <URL:http://classicvb.org/petition/>
> >
> >
Author
8 Apr 2005 1:49 PM
Herfried K. Wagner [MVP]
"Guy" <G**@discussions.microsoft.com> schrieb:
> I tried your suggestion and it still doesn work.
>
> I changed the code to :
>
>  strFileContentsHTML = strFileContentsHTML.Replace(vbLf, "<br>")
>  strFileContentsHTML = strFileContentsHTML.Replace(vbCrLf, "<br>")

The order of your replacements doesn't make much sense.  If you are already
replacing 'vbLf' characters with "<br>", the text doesn't contain 'vbCrLf'
character sequences any more.  Are you sure the stringbuilder contains the
characters you want to replace?

> Earlier I tested
>
>  strFileContentsHTML.Replace("a", "<br>")

This line won't work too because you never assign the return value of
'Replace' to a string variable.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
8 Apr 2005 8:21 PM
Jay B. Harlow [MVP - Outlook]
Herfried,
| 'StringBuilder.Replace' /returns/ a reference to a string builder which
| contains the final data:
Correct!

However! it also modifies the StringBuilder itself, and then returns a
reference to "Me".

Making the assignment:

| strFileContentsHtml = strFileContentsHtml.Replace(...)

unnecessary with StringBuilder as it is returning "Me", which has already
been modified. Unlike String.Replace that returns a new string.

The StringBuilder returns a reference to allow:

    strFileContentsHTML.Replace(vbLf, "<br>").Replace(vbCrLf,
"<br>").Replace(vbCr, "<br>")

More commonly I've seen:

    strFileContentsHTML.Append("Hello
").Append(firstName).Append(ControlChars.NewLine)

Which allows each "line" to be on a single source line, however I find it
more confusing then simply using a With statement or the original code.

Which enables C# to use:

    strFileContentsHTML.Append("Hello ")
        .Append(firstName)
        .Append(ControlChars.NewLine);

Which VB.NET does not allow

    strFileContentsHTML.Append("Hello ") _
        .Append(firstName) _
        .Append(ControlChars.NewLine)

However VB.NET has the With statement to cover the above, which IMHO is
better as it doesn't require the type's methods to return the "Me/this"
reference...

Hope this helps
Jay



Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:eOisanDPFHA.1176@TK2MSFTNGP12.phx.gbl...
| "Guy" <G**@discussions.microsoft.com> schrieb:
| > I'm trying to run this code :
| >
| >  strFileContentsHTML.Replace(vbLf, "<br>")
| >  strFileContentsHTML.Replace(vbCrLf, "<br>")
| >  strFileContentsHTML.Replace(vbCr, "<br>")
| >
| > It doesn't replace newline characters with <br>.
|
| 'StringBuilder.Replace' /returns/ a reference to a string builder which
| contains the final data:
|
| \\\
| strFileContentsHtml = strFileContentsHtml.Replace(...)
| ///
|
| --
| M S   Herfried K. Wagner
| M V P  <URL:http://dotnet.mvps.org/>
| V B   <URL:http://classicvb.org/petition/>
|
Author
8 Apr 2005 9:55 PM
Herfried K. Wagner [MVP]
Jay,

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@msn.com> schrieb:
> | 'StringBuilder.Replace' /returns/ a reference to a string builder which
> | contains the final data:
> Correct!
>
> However! it also modifies the StringBuilder itself, and then returns a
> reference to "Me".

Thank you for making me aware of that.  When I was pressing the "Send"
button I was almost sure that I missed something because I could not believe
that a new 'StringBuilder' is created when performing a simple replace
operation :-/.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>