By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,442 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Guy" <Gu*@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/>

Nov 21 '05 #2

P: n/a
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

"Herfried K. Wagner [MVP]" wrote:
"Guy" <Gu*@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/>

Nov 21 '05 #3

P: n/a
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
Nov 21 '05 #4

P: n/a
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
"Guy" <Gu*@discussions.microsoft.com> wrote in message
news:8F**********************************@microsof t.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" <Gu*@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/>

Nov 21 '05 #5

P: n/a
"Guy" <Gu*@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/>

Nov 21 '05 #6

P: n/a
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
"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

Nov 21 '05 #7

P: n/a
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.NewLi ne)

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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eO**************@TK2MSFTNGP12.phx.gbl...
| "Guy" <Gu*@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/>
|
Nov 21 '05 #8

P: n/a
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@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/>

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.