Android Code Snippet [B4X] Occurrences

Three functions that might be useful.

EDIT:
Given the right observation by @Mahares (post #4), I modified the first two functions to take "case insensitive" into account.

B4X:
'Returns the number of occurrences of Pattern in Text.
Sub OccurrCount(Pattern As String, Text As String, CaseSensitive As Boolean) As Int
    Dim MatcherX As Matcher
    Dim Counter As Int

    If CaseSensitive Then
        MatcherX = Regex.Matcher(Pattern, Text)
    Else
        MatcherX = Regex.Matcher2(Pattern, Regex.CASE_INSENSITIVE, Text)
    End If
  
    Counter = 0
    Do While MatcherX.Find
        Counter = Counter + 1
    Loop
 
    Return Counter
End Sub

B4X:
Sub OccurrIndexes(SubString As String, Text As String, CaseSensitive As Boolean) As List
    Dim lstResult As List : lstResult.Initialize
    
    If CaseSensitive = False Then
        SubString = SubString.ToLowerCase
        Text = Text.ToLowerCase
    End If
    
    Dim StartIndex As Int = Text.IndexOf2(SubString, 0)
    Dim Found As Boolean = StartIndex <> - 1
    
    Do While Found
        lstResult.Add(StartIndex)
        StartIndex = Text.IndexOf2(SubString.ToLowerCase, StartIndex + SubString.Length)
        Found = StartIndex <> - 1
    Loop
    
    Return lstResult
End Sub

B4X:
'Returns all numbers in Text.
Sub NumbersIn(Text As String) As List
    Dim lstResult As List : lstResult.Initialize
 
    Dim Matcher1 As Matcher = Regex.Matcher("\d+", Text)
    Do While Matcher1.Find
        lstResult.Add(Matcher1.Match)
    Loop
 
    Return lstResult
End Sub


Note: I know very little about regular expressions (sooner or later I'll study them SERIOUSLY), so I don't know if it's possible to simplify/improve these functions.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Sub OccurrIndexes
Just for fun, I asked Perplexity:

What this B4X function does? Sub OccurrIndexes(SubString As String, Text As String) As List Dim lstResult As List : lstResult.Initialize Dim StartIndex As Int = Text.IndexOf2(SubString, 0) Dim Found As Boolean = StartIndex <> - 1 Do While Found lstResult.Add(StartIndex) StartIndex = Text.IndexOf2(SubString, StartIndex + SubString.Length) Found = StartIndex <> - 1 Loop Return lstResult End Sub​


Senza nome.png

šŸ˜

It also added an example of using the function, although I didn't specifically ask for it.

It even used a smart-string!
 

Mahares

Expert
Licensed User
Longtime User
What this B4X function does? Sub OccurrIndexes
The only issue with looking for a substring inside a string is always how to treat lower and uppercase words:
Here is Luca' s function slightly modified:
Returns the positions of all occurrences of SubString in Text.
B4X:
Sub OccurrIndexes(SubString As String, Text As String, CaseSensitive As Boolean) As List
    Dim lstResult As List : lstResult.Initialize
    If CaseSensitive = False Then Text = Text.ToLowerCase  'mahares    
    Dim StartIndex As Int = Text.IndexOf2(SubString, 0)
    Dim Found As Boolean = StartIndex <> - 1
    Do While Found
        lstResult.Add(StartIndex)
        StartIndex = Text.IndexOf2(SubString, StartIndex + SubString.Length)
        Found = StartIndex <> - 1
    Loop  
    Return lstResult
End Sub
B4X:
Log(OccurrIndexes("o", "The quick brown fOx jumps over", False))
 

LucaMs

Expert
Licensed User
Longtime User
The only issue with looking for a substring inside a string is always how to treat lower and uppercase words:
Here is Luca' s function slightly modified:
Returns the positions of all occurrences of SubString in Text.
B4X:
Sub OccurrIndexes(SubString As String, Text As String, CaseSensitive As Boolean) As List
    Dim lstResult As List : lstResult.Initialize
    If CaseSensitive = False Then Text = Text.ToLowerCase  'mahares 
    Dim StartIndex As Int = Text.IndexOf2(SubString, 0)
    Dim Found As Boolean = StartIndex <> - 1
    Do While Found
        lstResult.Add(StartIndex)
        StartIndex = Text.IndexOf2(SubString, StartIndex + SubString.Length)
        Found = StartIndex <> - 1
    Loop
    Return lstResult
End Sub
B4X:
Log(OccurrIndexes("o", "The quick brown fOx jumps over", False))
RIGHT!


Thank you, @Mahares
 

LucaMs

Expert
Licensed User
Longtime User
The only issue with looking for a substring inside a string is always how to treat lower and uppercase words:
Here is Luca' s function slightly modified:
Returns the positions of all occurrences of SubString in Text.
B4X:
Sub OccurrIndexes(SubString As String, Text As String, CaseSensitive As Boolean) As List
    Dim lstResult As List : lstResult.Initialize
    If CaseSensitive = False Then Text = Text.ToLowerCase  'mahares   
    Dim StartIndex As Int = Text.IndexOf2(SubString, 0)
    Dim Found As Boolean = StartIndex <> - 1
    Do While Found
        lstResult.Add(StartIndex)
        StartIndex = Text.IndexOf2(SubString, StartIndex + SubString.Length)
        Found = StartIndex <> - 1
    Loop 
    Return lstResult
End Sub
B4X:
Log(OccurrIndexes("o", "The quick brown fOx jumps over", False))
It was also necessary to change the searched SubString to lower case, if CaseSensitive was False.
 
Top