|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
stringbuilder replace doesn't work on vbLf , vbCrLf , vbCrHi,
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 "Guy" <G**@discussions.microsoft.com> schrieb: 'StringBuilder.Replace' /returns/ a reference to a string builder which > 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>. 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/> 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/> > > 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 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 > > > Have you tried using the asci decimal value 
 or Chr(10) or Chr(13) instead of vbLf
and vbCrLf -- Show quoteHide quoteSteve Easton Microsoft MVP FrontPage 95isalive This site is best viewed.................. ...............................with a computer "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/> > > > > "Guy" <G**@discussions.microsoft.com> schrieb: The order of your replacements doesn't make much sense. If you are already > I tried your suggestion and it still doesn work. > > I changed the code to : > > strFileContentsHTML = strFileContentsHTML.Replace(vbLf, "<br>") > strFileContentsHTML = strFileContentsHTML.Replace(vbCrLf, "<br>") 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 This line won't work too because you never assign the return value of > > strFileContentsHTML.Replace("a", "<br>") '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/> Herfried,
| 'StringBuilder.Replace' /returns/ a reference to a string builder which However! it also modifies the StringBuilder itself, and then returns a | contains the final data: Correct! 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/> | Jay,
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@msn.com> schrieb: Thank you for making me aware of that. When I was pressing the "Send" > | '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". 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/> |
|||||||||||||||||||||||