Public Sub CompareTo1(S1 As String,S2 As String) As Int
Return S1.ToLowerCase.CompareTo(S2.ToLowerCase)
End Sub
2)
B4X:
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:
B4X:
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
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
My code is in a Code Module so I had to initialize my JavaOject somewhere using
B4X:
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
B4A does have a function. Do not sell it short. See below:
B4X:
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