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

G

Guest

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
 
H

Herfried K. Wagner [MVP]

Guy said:
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(...)
///
 
G

Guest

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
 
C

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
 
9

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
 
G

Guest

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
 
H

Herfried K. Wagner [MVP]

Guy said:
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.
 
J

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



| > 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/>
|
 
H

Herfried K. Wagner [MVP]

Jay,

Jay B. Harlow said:
| '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 :-/.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top