B4J Question how can i find if exists symbol inside a String ?

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
hi everyone !
how can i find if inside of my string exists some symbol or other chars ?

like this :
B4X:
mystring.findString("!@#$%¨&*()+=-[]~´;/:?}{`^<>|\")
have some function like this ? or function what find char by char to find a symbol ?
 

OliverA

Expert
Licensed User
Longtime User
B4X:
'
   Dim text, pattern As String
   pattern = "[!@#$%¨&*()+=\-\[\]~´;/:?}{`\^<>|\\]"
   text = "hello\world"
   Dim Matcher1 As Matcher
   Matcher1 = Regex.Matcher(pattern, text)
   Do While Matcher1.Find
       Log("Found: " & Matcher1.Match)
   Loop
   text = "hello^world"
   Matcher1 = Regex.Matcher(pattern, text)
   Do While Matcher1.Find
       Log("Found: " & Matcher1.Match)
   Loop
   text = "!@#$%¨&*()+=-[]~´;/:?}{`^<>|\"
   Log(text.Length)
   text = "hello!@#$%¨&*()+=-[]~´;/:?}{`^<>|\world"
   Matcher1 = Regex.Matcher(pattern, text)
   Dim found As Int = 0
   Do While Matcher1.Find
       Log("Found: " & Matcher1.Match)
       found = found + 1
   Loop
   Log(found)
Notes:
1) Using [] (character class) in a regular expression pattern you have to escape these four symbols: \ ] ^ -
Source: https://www.regular-expressions.info/charclass.html
2) Looks like Java/B4J also wants the following character escaped: [
Source: Error log message
3) The escape character is \
4) The code is just an adaptation of the code that pops up in the IDE when you start typing regex.matcher
 
Upvote 0
Top