Android Question String replace (case insenstive)

kmap

Member
Licensed User
Longtime User
How to replace a string in lowercase in string that contains the first string in capital letters.

example

fs="book"

ss="I like this Book"

ss=ss.replace(fs,"@Book")

Dont want to convert ss to lower case.
 

margret

Well-Known Member
Licensed User
Longtime User
You can use the Stuff command from the StringFunctions Library. There is a link in my sig.

See Below:

Stuff (CurrentText As String, InsertInString As String, RemoveFromString As String) As String
Used to replace text in a string with new text.
Example:

MyText = "I will tell you that joe is a good friend!"
MyText = SF.Stuff(MyText, "Joe Brown", "joe")
This function will return: "I will tell you that Joe Brown is a good friend!"
 
Upvote 0

kmap

Member
Licensed User
Longtime User
I want to replace all occurences(upper and lower case in a string)

MyText = "I will tell you that Joe is a good friend!"
MyText = SF.Stuff(MyText, "@joe", "joe")

will this work?

mytext has Joe in upper case

This function will return: "I will tell you that @Joe is a good friend!"
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Try this:

B4X:
Dim ss As String

ss = "I like this Book and hate this book but I love Books"
ss = ReplaceCaseInsensitive(ss, "book", "@Book")

Sub ReplaceCaseInsensitive(FullText As String, TxtToReplace As String, NewTxt As String) As String
    Dim Res As String = FullText
    Dim TxtToReplaceCap As String = Capital(TxtToReplace)
    Dim p0, p1, p2 As Int

    p0 = 0
    Do Until p0 = -1
        p1 = Res.IndexOf2(TxtToReplace, p0)
        p2 = Res.IndexOf2(TxtToReplaceCap, p0)
        If Not((p1 + p2) = -2) Then
            If p1 < p2 AND p1 <> -1 Then
                p0 = p1
            Else
                p0 = p2
            End If
            Res = Res.SubString2(0, p0) & NewTxt & Res.SubString(p0 + TxtToReplace.Length)
            p0 = p0 + NewTxt.Length
        Else
            p0 = -1
        End If
    Loop
   
    Return Res

End Sub

Sub Capital(Text As String) As String
    Dim Res As String
   
    If Text.Length = 0 Then
        Res = ""
    Else
        Dim First As String
        Dim Ascii As Int
       
        First = Text.SubString2(0,1)
        Ascii = Asc(First)
        If Ascii >= 97 AND Ascii <= 122 Then
            Ascii = Ascii - 32
            Res = Chr(Ascii)
            If Text.Length > 1 Then
                Res = Res & Text.SubString(1)
            End If
        Else
            Res = Text
        End If
    End If
   
    Return Res
   
End Sub
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Try this code:
B4X:
Sub RegexReplace(Pattern As String, Text As String, Replacement As String) As String

    Dim m As Matcher

    m = Regex.Matcher2(Pattern, Regex.CASE_INSENSITIVE, Text) 'Case Insensitive

    Dim r As Reflector

    r.Target = m

    Return r.RunMethod2("replaceAll", Replacement, "java.lang.String")

End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Thanks for your code, NJDude.

I tried to get these two useful functions, changing your, but Regex.Matcher(or 2) seems is case insensitive regardless of Regex.CASE_INSENSITIVE.
(?)

B4X:
Sub StringReplaceAll(Text As String, Pattern As String, Replacement As String, CaseSensitive As Boolean) As String

    Dim m As Matcher

    If CaseSensitive Then
        m = Regex.Matcher(Pattern, Text)
    Else
        m = Regex.Matcher2(Pattern, Regex.CASE_INSENSITIVE, Text)
    End If

    Dim r As Reflector

    r.Target = m

    Return r.RunMethod2("replaceAll", Replacement, "java.lang.String")

End Sub


Sub CapitalLetter(Text As String, Set As Boolean) As String
    Dim Res As String = Text

    If Text.Length > 0 Then

        Dim FirstChar As String
        Dim AsciiChar As Int
        Dim AsciiSet, AsciiShift As Int
 
        If Set Then
            AsciiSet = 97
            AsciiShift = -32
        Else
            AsciiSet = 65
            AsciiShift = 32
        End If
 
        ' Checks if first char is letter and must be changed.
        FirstChar = Text.SubString2(0,1)
        AsciiChar = Asc(FirstChar)
        If AsciiChar >= AsciiSet AND AsciiChar <= AsciiSet + 25 Then
            AsciiChar = AsciiChar + AsciiShift
            Res = Chr(AsciiChar)
            If Text.Length > 1 Then
                Res = Res & Text.SubString(1)
            End If
        End If

    End If

    Return Res

End Sub


P.S.
1) I've never had the patience to learn the useful Regex
2) I have the great desire to understand how to use the reflection well, in order to get the best of what the Android SDK provides, but I have not yet found a simple and well done tutorial.
 
Last edited:
Upvote 0
Top