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.
 

Star-Dust

Expert
Licensed User
Longtime User
And this?
B4X:
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:
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

Mahares

Expert
Licensed User
Longtime User
Can we carry this a little bit more by matching or not matching case:
B4X:
Log(CountOccurrences4("B4X community knows B4x and B4XPages ", "B4X", True))
B4X:
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…