aaronk Well-Known Member Licensed User Longtime User Feb 6, 2017 #1 Hi, If I have a string, how can you return only numbers and letters only ? For Example: B4X: Dim MyString as string = "ABC.1>234?" ' how can I make it only return ACB1234 only ? I know I can use: B4X: MyString = MyString.Replace(".","") However, I was hoping not to use the 'replace' function. Is there a way to make a string only contain letters and numbers only ?
Hi, If I have a string, how can you return only numbers and letters only ? For Example: B4X: Dim MyString as string = "ABC.1>234?" ' how can I make it only return ACB1234 only ? I know I can use: B4X: MyString = MyString.Replace(".","") However, I was hoping not to use the 'replace' function. Is there a way to make a string only contain letters and numbers only ?
Erel B4X founder Staff member Licensed User Longtime User Feb 6, 2017 #2 B4X: Sub OnlyNumbersOrLetters (s As String) As String Dim m As Matcher = Regex.Matcher("[a-zA-Z0-9]+", s) Dim sb As StringBuilder sb.Initialize Do While m.Find sb.Append(m.Match) Loop Return sb.ToString End Sub Upvote 0
B4X: Sub OnlyNumbersOrLetters (s As String) As String Dim m As Matcher = Regex.Matcher("[a-zA-Z0-9]+", s) Dim sb As StringBuilder sb.Initialize Do While m.Find sb.Append(m.Match) Loop Return sb.ToString End Sub
S sorex Expert Licensed User Longtime User Feb 6, 2017 #3 or B4X: Dim MyString As String = "ABC.1>234?" Log( Regex.Replace("[^a-zA-Z0-9]",MyString,"") ) Upvote 0