weird string problem

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

    #1

    weird string problem

    Dear all,

    I need to replace in a long string all chr(10) to chr(10)chr(13) if 13 is
    not behind 10.

    dim str as string=....

    If str.Chars(i) = vbLf And str.Chars(i + 1) <> vbCr Then

    str.Insert(i, vbCr)

    i = i + 1

    End If



    But if set a breakpoint on insert and look on str before and after insert I
    can see, that vbCr is just not inserted!!! ascii(str.Chars (i) ) and
    str.Chars(i+/-1) are not 13!!!

    What is wrong??

    Thanks,

    Boni


  • Chris Dunaway

    #2
    Re: weird string problem

    The answer is that strings are immutable, meaning they cannot change.
    The insert method returns a new strings with your changes, therefore
    you need to assign it back to your string variable:

    str = str.Insert(i,vb Cr)

    Comment

    • Boni

      #3
      Re: weird string problem

      Or noooo. Soo simple. I might need some sleep !
      Thanks Chris
      "Chris Dunaway" <dunawayc@gmail .com> schrieb im Newsbeitrag
      news:1124809487 .148083.308100@ f14g2000cwb.goo glegroups.com.. .[color=blue]
      > The answer is that strings are immutable, meaning they cannot change.
      > The insert method returns a new strings with your changes, therefore
      > you need to assign it back to your string variable:
      >
      > str = str.Insert(i,vb Cr)
      >[/color]


      Comment

      • Dragon

        #4
        Re: weird string problem

        Hi,

        String.Insert doesn't change the original string (as well as other
        String methods), but generates a new
        one instead.

        So,
        1. Use str = str.Insert(i,vb Cr) - or -
        2. Use StringBuilder for that.

        BTW, have you considered using RegEx for this kind of job? It's pretty
        simple:
        str = System.Text.Reg ularExpressions .Regex.Replace( str, "\x0a(?!\x0 d)",
        Chr(10) & Chr(13))

        Roman



        Comment

        • Boni

          #5
          Re: weird string problem

          Hi Dragon,
          I was thinking about regexes, but my feeling was to avoid regexes for simple
          things because of performance reasons. But as I see the building of temp
          string objects seems to take time too. So I switch to regex. I don't think
          that I will mention timing difference.

          Thank you for an advice,
          Boni
          "Dragon" <no@spam.please > schrieb im Newsbeitrag
          news:eFi2qX$pFH A.3424@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Hi,
          >
          > String.Insert doesn't change the original string (as well as other
          > String methods), but generates a new
          > one instead.
          >
          > So,
          > 1. Use str = str.Insert(i,vb Cr) - or -
          > 2. Use StringBuilder for that.
          >
          > BTW, have you considered using RegEx for this kind of job? It's pretty
          > simple:
          > str = System.Text.Reg ularExpressions .Regex.Replace( str, "\x0a(?!\x0 d)",
          > Chr(10) & Chr(13))
          >
          > Roman
          >
          >
          >[/color]


          Comment

          Working...