Android Question Counting specific characters in autocompleEditText

jojo12

Member
Licensed User
Longtime User
Hello,

What would be the simplest way to count a specific character, like "(" in an autocompleteEditText.text ?

I can't figure out. Code is welcomed.

Thanks for any answer.
 
And this?
B4X: Copy
Sub CountOccurrences2 (text As String, match As String) As Int
    Return text.Length - text.Replace(match, "").Length
End Sub
Small correction. In case you are looking for an expression and not a single character

B4X: Copy
Sub CountOccurrences3(text As String, match As String) As Int
      Return (text.Length - text.Replace(match, "").Length)/match.Length
End Sub
 
Last edited:
Upvote 0
Can we carry this a little bit more by matching or not matching case:
Code: Copy
Log(CountOccurrences4("B4X community knows B4x and B4XPages ", "B4X", True))
Code: Copy
Sub CountOccurrences4(text As String, match As String, matchcsase As Boolean) As Int
    If matchcsase Then
        Return (text.Length - text.Replace(match, "").Length)/match.Length
    End If
    Return (text.Length - text.tolowercase.Replace(match.ToLowerCase, "").Length)/match.Length
End Sub
 
Upvote 0
Back
Top