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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guy

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

    Hi,

    I'm trying to run this code :

    strFileContents HTML.Replace(vb Lf, "<br>")
    strFileContents HTML.Replace(vb CrLf, "<br>")
    strFileContents HTML.Replace(vb Cr, "<br>")

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

    Does anyone know how I should do this?

    Thanks,
    Guy

  • Herfried K. Wagner [MVP]

    #2
    Re: stringbuilder replace doesn't work on vbLf , vbCrLf , vbCr

    "Guy" <Guy@discussion s.microsoft.com > schrieb:[color=blue]
    > I'm trying to run this code :
    >
    > strFileContents HTML.Replace(vb Lf, "<br>")
    > strFileContents HTML.Replace(vb CrLf, "<br>")
    > strFileContents HTML.Replace(vb Cr, "<br>")
    >
    > It doesn't replace newline characters with <br>.[/color]

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

    \\\
    strFileContents Html = strFileContents Html.Replace(.. .)
    ///

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

    Comment

    • Guy

      #3
      Re: stringbuilder replace doesn't work on vbLf , vbCrLf , vbCr

      Thanks for the repsonse Herfried.
      I tried your suggestion and it still doesn work.

      I changed the code to :

      strFileContents HTML = strFileContents HTML.Replace(vb Lf, "<br>")
      strFileContents HTML = strFileContents HTML.Replace(vb CrLf, "<br>")
      strFileContents HTML = strFileContents HTML.Replace(vb Cr, "<br>")

      Earlier I tested

      strFileContents HTML.Replace("a ", "<br>")

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

      Guy



      "Herfried K. Wagner [MVP]" wrote:
      [color=blue]
      > "Guy" <Guy@discussion s.microsoft.com > schrieb:[color=green]
      > > I'm trying to run this code :
      > >
      > > strFileContents HTML.Replace(vb Lf, "<br>")
      > > strFileContents HTML.Replace(vb CrLf, "<br>")
      > > strFileContents HTML.Replace(vb Cr, "<br>")
      > >
      > > It doesn't replace newline characters with <br>.[/color]
      >
      > 'StringBuilder. Replace' /returns/ a reference to a string builder which
      > contains the final data:
      >
      > \\\
      > strFileContents Html = strFileContents Html.Replace(.. .)
      > ///
      >
      > --
      > M S Herfried K. Wagner
      > M V P <URL:http://dotnet.mvps.org/>
      > V B <URL:http://classicvb.org/petition/>
      >
      >[/color]

      Comment

      • Cor Ligthert

        #4
        Re: stringbuilder replace doesn't work on vbLf , vbCrLf , vbCr

        Guy,

        Strange, this test
        \\\
        Dim strFileContents HTML As New System.Text.Str ingBuilder("Hel lo" & vbLf & _
        "how" & vbCrLf & "are" & vbCr & "you")
        strFileContents HTML = strFileContents HTML.Replace(vb Lf, "<br>")
        strFileContents HTML = strFileContents HTML.Replace(vb CrLf, "<br>")
        strFileContents HTML = strFileContents HTML.Replace(vb Cr, "<br>")
        Debug.WriteLine (strFileContent sHTML.ToString)
        ///
        Has for me as result.
        Hello<br>how<br ><br>are<br>y ou

        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


        Comment

        • 95isalive

          #5
          Re: stringbuilder replace doesn't work on vbLf , vbCrLf , vbCr

          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" <Guy@discussion s.microsoft.com > wrote in message
          news:8F7FDEF6-062A-4475-BF59-6779A114F532@mi crosoft.com...[color=blue]
          > Thanks for the repsonse Herfried.
          > I tried your suggestion and it still doesn work.
          >
          > I changed the code to :
          >
          > strFileContents HTML = strFileContents HTML.Replace(vb Lf, "<br>")
          > strFileContents HTML = strFileContents HTML.Replace(vb CrLf, "<br>")
          > strFileContents HTML = strFileContents HTML.Replace(vb Cr, "<br>")
          >
          > Earlier I tested
          >
          > strFileContents HTML.Replace("a ", "<br>")
          >
          > and it worked fine. The problem seems to be only with the newline characters.
          >
          > Guy
          >
          >
          >
          > "Herfried K. Wagner [MVP]" wrote:
          >[color=green]
          > > "Guy" <Guy@discussion s.microsoft.com > schrieb:[color=darkred]
          > > > I'm trying to run this code :
          > > >
          > > > strFileContents HTML.Replace(vb Lf, "<br>")
          > > > strFileContents HTML.Replace(vb CrLf, "<br>")
          > > > strFileContents HTML.Replace(vb Cr, "<br>")
          > > >
          > > > It doesn't replace newline characters with <br>.[/color]
          > >
          > > 'StringBuilder. Replace' /returns/ a reference to a string builder which
          > > contains the final data:
          > >
          > > \\\
          > > strFileContents Html = strFileContents Html.Replace(.. .)
          > > ///
          > >
          > > --
          > > M S Herfried K. Wagner
          > > M V P <URL:http://dotnet.mvps.org/>
          > > V B <URL:http://classicvb.org/petition/>
          > >
          > >[/color][/color]


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: stringbuilder replace doesn't work on vbLf , vbCrLf , vbCr

            "Guy" <Guy@discussion s.microsoft.com > schrieb:[color=blue]
            > I tried your suggestion and it still doesn work.
            >
            > I changed the code to :
            >
            > strFileContents HTML = strFileContents HTML.Replace(vb Lf, "<br>")
            > strFileContents HTML = strFileContents HTML.Replace(vb CrLf, "<br>")[/color]

            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?
            [color=blue]
            > Earlier I tested
            >
            > strFileContents HTML.Replace("a ", "<br>")[/color]

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

            Comment

            • Guy

              #7
              Re: stringbuilder replace doesn't work on vbLf , vbCrLf , vbCr

              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.Rea dLine

              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:
              [color=blue]
              > Guy,
              >
              > Strange, this test
              > \\\
              > Dim strFileContents HTML As New System.Text.Str ingBuilder("Hel lo" & vbLf & _
              > "how" & vbCrLf & "are" & vbCr & "you")
              > strFileContents HTML = strFileContents HTML.Replace(vb Lf, "<br>")
              > strFileContents HTML = strFileContents HTML.Replace(vb CrLf, "<br>")
              > strFileContents HTML = strFileContents HTML.Replace(vb Cr, "<br>")
              > Debug.WriteLine (strFileContent sHTML.ToString)
              > ///
              > Has for me as result.
              > Hello<br>how<br ><br>are<br>y ou
              >
              > 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
              >
              >
              >[/color]

              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: stringbuilder replace doesn't work on vbLf , vbCrLf , vbCr

                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:

                | strFileContents Html = strFileContents Html.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:

                strFileContents HTML.Replace(vb Lf, "<br>").Replace (vbCrLf,
                "<br>").Replace (vbCr, "<br>")

                More commonly I've seen:

                strFileContents HTML.Append("He llo
                ").Append(first Name).Append(Co ntrolChars.NewL ine)

                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:

                strFileContents HTML.Append("He llo ")
                .Append(firstNa me)
                .Append(Control Chars.NewLine);

                Which VB.NET does not allow

                strFileContents HTML.Append("He llo ") _
                .Append(firstNa me) _
                .Append(Control Chars.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]" <hirf-spam-me-here@gmx.at> wrote in message
                news:eOisanDPFH A.1176@TK2MSFTN GP12.phx.gbl...
                | "Guy" <Guy@discussion s.microsoft.com > schrieb:
                | > I'm trying to run this code :
                | >
                | > strFileContents HTML.Replace(vb Lf, "<br>")
                | > strFileContents HTML.Replace(vb CrLf, "<br>")
                | > strFileContents HTML.Replace(vb Cr, "<br>")
                | >
                | > It doesn't replace newline characters with <br>.
                |
                | 'StringBuilder. Replace' /returns/ a reference to a string builder which
                | contains the final data:
                |
                | \\\
                | strFileContents Html = strFileContents Html.Replace(.. .)
                | ///
                |
                | --
                | M S Herfried K. Wagner
                | M V P <URL:http://dotnet.mvps.org/>
                | V B <URL:http://classicvb.org/petition/>
                |


                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: stringbuilder replace doesn't work on vbLf , vbCrLf , vbCr

                  Jay,

                  "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> schrieb:[color=blue]
                  > | '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".[/color]

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

                  Comment

                  Working...