Home All Groups Group Topic Archive Search About

best way for Replace insensitive in strings

Author
13 Jun 2006 8:37 PM
pamelafluente
I need to replace all the occurences of a string within another string
(or stringbuilder):

    Function ReplaceInsensitive(ByVal InputString As String, _
                                             ByVal SubstringReplaced As
String, _
                                             ByVal Replacement As
String) As String
'...
    End Function

I wish the search for "SubstringReplaced" 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

Author
13 Jun 2006 8:36 PM
Oenone
pamelaflue***@libero.it wrote:
> I wish the search for "SubstringReplaced" to be case insensitive.

\\\
    Function ReplaceInsensitive(ByVal InputString As String, _
                                         ByVal SubstringReplaced As String,
_
                                         ByVal Replacement As String) As
String

        Return Replace(InputString, SubStringReplaced, _
                    Replacement, Compare:=CompareMethod.Text)

    End Function
///

--

(O)enone
Author
13 Jun 2006 8:57 PM
Ahmed
NOT TESTED

Function ReplaceInsensitive(ByVal InputString As String, _
                                                 ByVal
SubstringReplaced As String, _
                                                 ByVal Replacement As
String) As String

        Return InputString.Replace(SubstringReplaced, Replacement)
End Function


pamelaflue***@libero.it wrote:
Show quoteHide quote
> I need to replace all the occurences of a string within another string
> (or stringbuilder):
>
>     Function ReplaceInsensitive(ByVal InputString As String, _
>                                              ByVal SubstringReplaced As
> String, _
>                                              ByVal Replacement As
> String) As String
> '...
>     End Function
>
> I wish the search for "SubstringReplaced" 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
Author
13 Jun 2006 10:51 PM
pamelafluente
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:=CompareMethod.Binary)


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

    Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
                           ByVal SubstringReplaced As String, _
                           ByVal Replacement As String)
        '...
    End Sub


Thank you very much.

-Pam
Author
13 Jun 2006 11:11 PM
Ahmed
Samething,
sb.Replace(substringreplaced, replacement)
pamelaflue***@libero.it wrote:
Show quoteHide quote
> 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:=CompareMethod.Binary)
>
>
> Any hint, instead, for the case when a StringBuilder is used?
>
>     Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
>                            ByVal SubstringReplaced As String, _
>                            ByVal Replacement As String)
>         '...
>     End Sub
>
>
> Thank you very much.
>
> -Pam
Author
13 Jun 2006 11:21 PM
pamelafluente
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:

Show quoteHide quote
> Samething,
> sb.Replace(substringreplaced, replacement)
> pamelaflue***@libero.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:=CompareMethod.Binary)
> >
> >
> > Any hint, instead, for the case when a StringBuilder is used?
> >
> >     Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
> >                            ByVal SubstringReplaced As String, _
> >                            ByVal Replacement As String)
> >         '...
> >     End Sub
> >
> >
> > Thank you very much.
> >
> > -Pam
Author
14 Jun 2006 12:28 AM
Ahmed
Return Replace(sb.tostring, SubStringReplaced, _
                    Replacement, Compare:=CompareMethod.Text)

pamelaflue***@libero.it wrote:
Show quoteHide quote
> 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:
>
> > Samething,
> > sb.Replace(substringreplaced, replacement)
> > pamelaflue***@libero.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:=CompareMethod.Binary)
> > >
> > >
> > > Any hint, instead, for the case when a StringBuilder is used?
> > >
> > >     Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
> > >                            ByVal SubstringReplaced As String, _
> > >                            ByVal Replacement As String)
> > >         '...
> > >     End Sub
> > >
> > >
> > > Thank you very much.
> > >
> > > -Pam
Author
14 Jun 2006 12:39 AM
pamelafluente
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:

Show quoteHide quote
> Return Replace(sb.tostring, SubStringReplaced, _
>                     Replacement, Compare:=CompareMethod.Text)
>
Author
14 Jun 2006 1:09 AM
GhostInAK
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

Show quoteHide quote
> 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:
>
>> Return Replace(sb.tostring, SubStringReplaced, _
>> Replacement, Compare:=CompareMethod.Text)
Author
14 Jun 2006 6:40 AM
Cor Ligthert [MVP]
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


<pamelaflue***@libero.it> schreef in bericht
Show quoteHide quote
news:1150240880.238923.147040@f6g2000cwb.googlegroups.com...
> 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:
>
>> Samething,
>> sb.Replace(substringreplaced, replacement)
>> pamelaflue***@libero.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:=CompareMethod.Binary)
>> >
>> >
>> > Any hint, instead, for the case when a StringBuilder is used?
>> >
>> >     Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
>> >                            ByVal SubstringReplaced As String, _
>> >                            ByVal Replacement As String)
>> >         '...
>> >     End Sub
>> >
>> >
>> > Thank you very much.
>> >
>> > -Pam
>
Author
14 Jun 2006 8:56 AM
tommaso.gastaldi
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.ToUpper()
        strTemp = strExpression.ToUpper()
        lngPosition = strTemp.IndexOf(strSearch)
        Do While lngPosition >= 0
            strReturn = strReturn + strExpression.SubString(0,lngPosition) +
strReplace
            strExpression =
strExpression.SubString(lngPosition+strSearch.Length)
            strTemp = strTemp.SubString(lngPosition+strSearch.Length)
            lngPosition = strTemp.IndexOf(strSearch)
        Loop
        strReturn = strReturn + strExpression
    Else
        '--- vbBinaryCompare
        strReturn = strExpression.Replace(strSearch,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.ToUpper()
        lngPosition = strTemp.IndexOf(strSearch.ToUpper())
        If lngPosition >= 0 Then
            strTemp = strExpression.Remove(lngPosition,strSearch.Length)
            strTemp = strTemp.Insert(lngPosition,strReplace)
            strReturn = CReplace(strTemp,strSearch,strReplace,intMode)
        Else
            strReturn = strExpression
        End If
    Else
        '--- vbBinaryCompare
        strReturn = strExpression.Replace(strSearch,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(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 strSearch=GetCaseInsensitiveSearch(strSearch)

    strReturn = Regex.Replace(strExpression,strSearch,strReplace)

    CReplace = strReturn

End Function

' Creates a case-insensitive regular expression search string
' For Example:
'    "[fF][oO][oO][bB][aA][rR]"= GetCaseInsensitiveSearch("FooBar")
Public Function GetCaseInsensitiveSearch(strSearch 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.Length-1

        chrCurrent=strSearch.Chars(intCounter)
        chrLower = char.ToLower(chrCurrent)
        chrUpper = char.ToUpper(chrCurrent)
        If chrUpper = chrLower Then
            strReturn = strReturn + chrCurrent
        Else
            strReturn = strReturn + "[" + chrLower + chrUpper + "]"
        End If
    Next
    GetCaseInsensitiveSearch = strReturn
End Function


for instance:

http://authors.aspalliance.com/bbilbro/DesktopDefault.aspx?tabindex=1&tabid=7&ArticleID=4






Cor Ligthert [MVP] ha scritto:

Show quoteHide quote
> 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
>
>
> <pamelaflue***@libero.it> schreef in bericht
> news:1150240880.238923.147040@f6g2000cwb.googlegroups.com...
> > 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:
> >
> >> Samething,
> >> sb.Replace(substringreplaced, replacement)
> >> pamelaflue***@libero.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:=CompareMethod.Binary)
> >> >
> >> >
> >> > Any hint, instead, for the case when a StringBuilder is used?
> >> >
> >> >     Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
> >> >                            ByVal SubstringReplaced As String, _
> >> >                            ByVal Replacement As String)
> >> >         '...
> >> >     End Sub
> >> >
> >> >
> >> > Thank you very much.
> >> >
> >> > -Pam
> >