Android Question CompareToIgnoreCase

Robert Valentino

Well-Known Member
Licensed User
Longtime User
When I want to compare to items case insensitive I usually lower case both string and then do a compare

Java has a function to compare to strings ignoring case and return an INT

Is it more effective to do then Java call rather then lower casing string all the time to compare?

Amazing that our String function does not have a CompareToIgnoreCase

#If JAVA
public int CompareToIgnoreCase(String S1, String S2) {

return S1.compareToIgnoreCase(S2);
}
#End If
 
1)
Code: Copy
Public Sub CompareTo1(S1 As String,S2 As String) As Int
    Return S1.ToLowerCase.CompareTo(S2.ToLowerCase)
End Sub

2)
Code: Copy
public Sub CompareTo2(S1 As String,S2 As String) As Int
    Dim J As JavaObject = Me

    Dim L As Int =j.RunMethod("CompareToIgnoreCase", Array(S1,S2))

    Return l
End Sub

#If JAVA
public int CompareToIgnoreCase(String S1, String S2) {

return S1.compareToIgnoreCase(S2);
}
#End If

I do not know which one is better, do some tests on a thousand comparisons and see who takes less time:
Code: Copy
Sub Test
    'First
    Dim L1 As Long= DateTime.Now
    For I=0 To 999
        CompareTo1(StringGenerator,StringGenerator)
    Next
    L1=DateTime.Now-L1
    
    'Second
    Dim L2 As Long= DateTime.Now
    For I=0 To 999
        CompareTo2(StringGenerator,StringGenerator)
    Next
    L2=DateTime.Now-L2
    
    Log("First: " & L1 & " - Second:" & L2)
    
End Sub

Sub StringGenerator As String
    Dim S As String =""
    
    For i=1 To 20
        S= S & Chr(Rnd(65,91))
    Next
    
    Return S
End Sub
 
Last edited:
Upvote 0
I do not know which one is better, do some tests on a thousand comparisons and see who takes less time:
Aren't you missing this line from your code: J.InitializeContext
Anyway, I added it to your code and tested the comparison between the 2 methods (using lowercase and inlinejava a 1000 times) on one device and the difference is not terribly significant. Method1 vs Method2
Test1: 2517 vs 2668
Test2: 3106 vs 2907
Test3: 2619 vs 2700
 
Upvote 0
My code is in a Code Module so I had to initialize my JavaOject somewhere using
Code: Copy
Sub Process_Globals
      Private StringCompareIgnoreCase            As JavaObject
end sub

Public Sub Initialize
         StringCompareIgnoreCase.InitializeNewInstance(Application.PackageName & ".ctree", Null)           
end sub

Appreciate the effort - just so surprised we do not have a compareToIngoreCase by default

What I am doing is inserting into a Tree list
 
Upvote 0
Since the differences in execution times on 1000 comparisons is minimal, use this:
Code: Copy
Public Sub CompareTo1(S1 As String,S2 As String) As Int
    Return S1.ToLowerCase.CompareTo(S2.ToLowerCase)
End Sub
you will not need to use JavaObject to initialize it
 
Upvote 0
just so surprised we do not have a compareToIngoreCase by default
B4A does have a function. Do not sell it short. See below:
Code: Copy
Dim str1 As String="New York" , str2 As String="New Yrk"
    Log(str1.EqualsIgnoreCase(str2))  'retuns false
   
    Dim str1 As String="tel aviv" , str2 As String="Tel Aviv"
    Log(str1.EqualsIgnoreCase(str2))  'retuns true
 
Upvote 0
Back
Top