best way for Replace insensitive in strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pamelafluente@libero.it

    #1

    best way for Replace insensitive in strings

    I need to replace all the occurences of a string within another string
    (or stringbuilder):

    Function ReplaceInsensit ive(ByVal InputString As String, _
    ByVal SubstringReplac ed As
    String, _
    ByVal Replacement As
    String) As String
    '...
    End Function

    I wish the search for "SubstringRepla ced" to be case insensitive.

    I can think of various way to do it, but all I can think is quite
    awkward. Can anybody suggest a good and fast method? There must be
    something built in the language, but I am missing it

    -Pam

  • Oenone

    #2
    Re: best way for Replace insensitive in strings

    pamelafluente@l ibero.it wrote:[color=blue]
    > I wish the search for "SubstringRepla ced" to be case insensitive.[/color]

    \\\
    Function ReplaceInsensit ive(ByVal InputString As String, _
    ByVal SubstringReplac ed As String,
    _
    ByVal Replacement As String) As
    String

    Return Replace(InputSt ring, SubStringReplac ed, _
    Replacement, Compare:=Compar eMethod.Text)

    End Function
    ///

    --

    (O)enone


    Comment

    • Ahmed

      #3
      Re: best way for Replace insensitive in strings

      NOT TESTED

      Function ReplaceInsensit ive(ByVal InputString As String, _
      ByVal
      SubstringReplac ed As String, _
      ByVal Replacement As
      String) As String

      Return InputString.Rep lace(SubstringR eplaced, Replacement)
      End Function


      pamelafluente@l ibero.it wrote:[color=blue]
      > I need to replace all the occurences of a string within another string
      > (or stringbuilder):
      >
      > Function ReplaceInsensit ive(ByVal InputString As String, _
      > ByVal SubstringReplac ed As
      > String, _
      > ByVal Replacement As
      > String) As String
      > '...
      > End Function
      >
      > I wish the search for "SubstringRepla ced" to be case insensitive.
      >
      > I can think of various way to do it, but all I can think is quite
      > awkward. Can anybody suggest a good and fast method? There must be
      > something built in the language, but I am missing it
      >
      > -Pam[/color]

      Comment

      • pamelafluente@libero.it

        #4
        Re: best way for Replace insensitive in strings


        The version provided by (O)enone works fine. Thanks for the help!

        (Ahmed suggestion is on the right track, omitting the CompareMethod is
        like: Compare:=Compar eMethod.Binary)


        Any hint, instead, for the case when a StringBuilder is used?

        Sub ReplaceInsensit ive(ByVal Sb As System.Text.Str ingBuilder, _
        ByVal SubstringReplac ed As String, _
        ByVal Replacement As String)
        '...
        End Sub


        Thank you very much.

        -Pam

        Comment

        • Ahmed

          #5
          Re: best way for Replace insensitive in strings

          Samething,
          sb.Replace(subs tringreplaced, replacement)
          pamelafluente@l ibero.it wrote:[color=blue]
          > The version provided by (O)enone works fine. Thanks for the help!
          >
          > (Ahmed suggestion is on the right track, omitting the CompareMethod is
          > like: Compare:=Compar eMethod.Binary)
          >
          >
          > Any hint, instead, for the case when a StringBuilder is used?
          >
          > Sub ReplaceInsensit ive(ByVal Sb As System.Text.Str ingBuilder, _
          > ByVal SubstringReplac ed As String, _
          > ByVal Replacement As String)
          > '...
          > End Sub
          >
          >
          > Thank you very much.
          >
          > -Pam[/color]

          Comment

          • pamelafluente@libero.it

            #6
            Re: best way for Replace insensitive in strings

            mmm ... no, remember the condition that it must be "case insensitive".
            I don't see an option to specify it with the StringBuilder Replace
            method (but I may be missing it (?) ).

            -P

            Ahmed ha scritto:
            [color=blue]
            > Samething,
            > sb.Replace(subs tringreplaced, replacement)
            > pamelafluente@l ibero.it wrote:[color=green]
            > > The version provided by (O)enone works fine. Thanks for the help!
            > >
            > > (Ahmed suggestion is on the right track, omitting the CompareMethod is
            > > like: Compare:=Compar eMethod.Binary)
            > >
            > >
            > > Any hint, instead, for the case when a StringBuilder is used?
            > >
            > > Sub ReplaceInsensit ive(ByVal Sb As System.Text.Str ingBuilder, _
            > > ByVal SubstringReplac ed As String, _
            > > ByVal Replacement As String)
            > > '...
            > > End Sub
            > >
            > >
            > > Thank you very much.
            > >
            > > -Pam[/color][/color]

            Comment

            • Ahmed

              #7
              Re: best way for Replace insensitive in strings

              Return Replace(sb.tost ring, SubStringReplac ed, _
              Replacement, Compare:=Compar eMethod.Text)

              pamelafluente@l ibero.it wrote:[color=blue]
              > mmm ... no, remember the condition that it must be "case insensitive".
              > I don't see an option to specify it with the StringBuilder Replace
              > method (but I may be missing it (?) ).
              >
              > -P
              >
              > Ahmed ha scritto:
              >[color=green]
              > > Samething,
              > > sb.Replace(subs tringreplaced, replacement)
              > > pamelafluente@l ibero.it wrote:[color=darkred]
              > > > The version provided by (O)enone works fine. Thanks for the help!
              > > >
              > > > (Ahmed suggestion is on the right track, omitting the CompareMethod is
              > > > like: Compare:=Compar eMethod.Binary)
              > > >
              > > >
              > > > Any hint, instead, for the case when a StringBuilder is used?
              > > >
              > > > Sub ReplaceInsensit ive(ByVal Sb As System.Text.Str ingBuilder, _
              > > > ByVal SubstringReplac ed As String, _
              > > > ByVal Replacement As String)
              > > > '...
              > > > End Sub
              > > >
              > > >
              > > > Thank you very much.
              > > >
              > > > -Pam[/color][/color][/color]

              Comment

              • pamelafluente@libero.it

                #8
                Re: best way for Replace insensitive in strings

                mmm ... Nice try Ahmed, but note that we are working with a
                StringBuilder (infact, I proposed a *Sub* and *not* a Function). The
                usefulness of a stringbuilder is that we have an immutable string, if
                we convert it to a string, there is no point in using a StringBuilder.

                -Pam

                Ahmed ha scritto:
                [color=blue]
                > Return Replace(sb.tost ring, SubStringReplac ed, _
                > Replacement, Compare:=Compar eMethod.Text)
                >[/color]

                Comment

                • GhostInAK

                  #9
                  Re: best way for Replace insensitive in strings

                  Hello Pam-o,

                  Yer changing schtuff in the string by the very definition of "replace", so
                  your precious immutable string.. aint so immuted anymore.

                  -Boo
                  [color=blue]
                  > mmm ... Nice try Ahmed, but note that we are working with a
                  > StringBuilder (infact, I proposed a *Sub* and *not* a Function). The
                  > usefulness of a stringbuilder is that we have an immutable string, if
                  > we convert it to a string, there is no point in using a StringBuilder.
                  >
                  > -Pam
                  >
                  > Ahmed ha scritto:
                  >[color=green]
                  >> Return Replace(sb.tost ring, SubStringReplac ed, _
                  >> Replacement, Compare:=Compar eMethod.Text)[/color][/color]


                  Comment

                  • Cor Ligthert [MVP]

                    #10
                    Re: best way for Replace insensitive in strings

                    Pamela,

                    If it are only words with a first upercase, I would just do it twice.

                    Otherwise Regex will be your needed tool.

                    RegexLib


                    Expresso


                    It needs a real hobbyist to help you then with your problem.

                    I hope this helps a little bit?

                    Cor


                    <pamelafluente@ libero.it> schreef in bericht
                    news:1150240880 .238923.147040@ f6g2000cwb.goog legroups.com...[color=blue]
                    > mmm ... no, remember the condition that it must be "case insensitive".
                    > I don't see an option to specify it with the StringBuilder Replace
                    > method (but I may be missing it (?) ).
                    >
                    > -P
                    >
                    > Ahmed ha scritto:
                    >[color=green]
                    >> Samething,
                    >> sb.Replace(subs tringreplaced, replacement)
                    >> pamelafluente@l ibero.it wrote:[color=darkred]
                    >> > The version provided by (O)enone works fine. Thanks for the help!
                    >> >
                    >> > (Ahmed suggestion is on the right track, omitting the CompareMethod is
                    >> > like: Compare:=Compar eMethod.Binary)
                    >> >
                    >> >
                    >> > Any hint, instead, for the case when a StringBuilder is used?
                    >> >
                    >> > Sub ReplaceInsensit ive(ByVal Sb As System.Text.Str ingBuilder, _
                    >> > ByVal SubstringReplac ed As String, _
                    >> > ByVal Replacement As String)
                    >> > '...
                    >> > End Sub
                    >> >
                    >> >
                    >> > Thank you very much.
                    >> >
                    >> > -Pam[/color][/color]
                    >[/color]


                    Comment

                    • tommaso.gastaldi@uniroma1.it

                      #11
                      Re: best way for Replace insensitive in strings


                      A special award to who finds the most efficient method :)

                      (grabbed from the web, easily adaptable to stringbuilder logic)

                      -tom



                      '--- Custom Replace Function CReplace
                      '--- VB.NET Loop Version
                      '--- intMode = 0 = Case-Sensitive
                      '--- intMode = 1 = Case-Insensitive
                      Function CReplace(ByVal strExpression As String, _
                      ByVal strSearch as String, _
                      strReplace As String, _
                      intMode as Integer _
                      ) As String
                      Dim strReturn as String
                      Dim lngPosition As Long
                      Dim strTemp As String

                      If intMode = 1 Then '--- vbTextCompare
                      strReturn = ""
                      strSearch = strSearch.ToUpp er()
                      strTemp = strExpression.T oUpper()
                      lngPosition = strTemp.IndexOf (strSearch)
                      Do While lngPosition >= 0
                      strReturn = strReturn + strExpression.S ubString(0,lngP osition) +
                      strReplace
                      strExpression =
                      strExpression.S ubString(lngPos ition+strSearch .Length)
                      strTemp = strTemp.SubStri ng(lngPosition+ strSearch.Lengt h)
                      lngPosition = strTemp.IndexOf (strSearch)
                      Loop
                      strReturn = strReturn + strExpression
                      Else
                      '--- vbBinaryCompare
                      strReturn = strExpression.R eplace(strSearc h,strReplace)
                      End If

                      CReplace = strReturn
                      End Function




                      '--- Custom Replace Function CReplace
                      '--- VB.NET Recursive Version
                      '--- intMode = 0 = Case-Sensitive
                      '--- intMode = 1 = Case-Insensitive
                      Function CReplace( strExpression As String, _
                      strSearch as String, _
                      strReplace As String, _
                      intMode as Integer _
                      ) As String
                      Dim strReturn as String
                      Dim lngPosition As Long
                      Dim strTemp As String

                      If intMode = 1 Then '--- vbTextCompare
                      strTemp = strExpression.T oUpper()
                      lngPosition = strTemp.IndexOf (strSearch.ToUp per())
                      If lngPosition >= 0 Then
                      strTemp = strExpression.R emove(lngPositi on,strSearch.Le ngth)
                      strTemp = strTemp.Insert( lngPosition,str Replace)
                      strReturn = CReplace(strTem p,strSearch,str Replace,intMode )
                      Else
                      strReturn = strExpression
                      End If
                      Else
                      '--- vbBinaryCompare
                      strReturn = strExpression.R eplace(strSearc h,strReplace)
                      End If

                      CReplace = strReturn
                      End Function



                      '--- Custom Replace Function CReplace
                      '--- VB.NET RegExp Version
                      '--- intMode = 0 = Case-Sensitive
                      '--- intMode = 1 = Case-Insensitive
                      Public Function CReplace(strExp ression As String, _
                      ByVal strSearch As String, _
                      strReplace As String, _
                      intMode as Integer _
                      ) As String

                      Dim strReturn As String
                      Dim lngPosition As Long
                      Dim strTemp as String

                      If intMode=1 Then strSearch=GetCa seInsensitiveSe arch(strSearch)

                      strReturn = Regex.Replace(s trExpression,st rSearch,strRepl ace)

                      CReplace = strReturn

                      End Function

                      ' Creates a case-insensitive regular expression search string
                      ' For Example:
                      ' "[fF][oO][oO][bB][aA][rR]"= GetCaseInsensit iveSearch("FooB ar")
                      Public Function GetCaseInsensit iveSearch(strSe arch As String) As String
                      Dim strReturn As New String(")
                      Dim chrCurrent As char
                      Dim chrLower As char
                      Dim chrUpper As char
                      Dim intCounter As Integer

                      For intCounter = 0 To strSearch.Lengt h-1

                      chrCurrent=strS earch.Chars(int Counter)
                      chrLower = char.ToLower(ch rCurrent)
                      chrUpper = char.ToUpper(ch rCurrent)
                      If chrUpper = chrLower Then
                      strReturn = strReturn + chrCurrent
                      Else
                      strReturn = strReturn + "[" + chrLower + chrUpper + "]"
                      End If
                      Next
                      GetCaseInsensit iveSearch = strReturn
                      End Function


                      for instance:








                      Cor Ligthert [MVP] ha scritto:
                      [color=blue]
                      > Pamela,
                      >
                      > If it are only words with a first upercase, I would just do it twice.
                      >
                      > Otherwise Regex will be your needed tool.
                      >
                      > RegexLib
                      > http://www.regexlib.com/Default.aspx
                      >
                      > Expresso
                      > http://www.ultrapico.com/Expresso.htm
                      >
                      > It needs a real hobbyist to help you then with your problem.
                      >
                      > I hope this helps a little bit?
                      >
                      > Cor
                      >
                      >
                      > <pamelafluente@ libero.it> schreef in bericht
                      > news:1150240880 .238923.147040@ f6g2000cwb.goog legroups.com...[color=green]
                      > > mmm ... no, remember the condition that it must be "case insensitive".
                      > > I don't see an option to specify it with the StringBuilder Replace
                      > > method (but I may be missing it (?) ).
                      > >
                      > > -P
                      > >
                      > > Ahmed ha scritto:
                      > >[color=darkred]
                      > >> Samething,
                      > >> sb.Replace(subs tringreplaced, replacement)
                      > >> pamelafluente@l ibero.it wrote:
                      > >> > The version provided by (O)enone works fine. Thanks for the help!
                      > >> >
                      > >> > (Ahmed suggestion is on the right track, omitting the CompareMethod is
                      > >> > like: Compare:=Compar eMethod.Binary)
                      > >> >
                      > >> >
                      > >> > Any hint, instead, for the case when a StringBuilder is used?
                      > >> >
                      > >> > Sub ReplaceInsensit ive(ByVal Sb As System.Text.Str ingBuilder, _
                      > >> > ByVal SubstringReplac ed As String, _
                      > >> > ByVal Replacement As String)
                      > >> > '...
                      > >> > End Sub
                      > >> >
                      > >> >
                      > >> > Thank you very much.
                      > >> >
                      > >> > -Pam[/color]
                      > >[/color][/color]

                      Comment

                      Working...