Wish TRIM function

ciarli

Member
Licensed User
Longtime User
I'd like to have three functions: LTrim for the left spaces, RTrim for the right spaces and Trim for all spaces.
Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Trim is already supported:
B4X:
Dim s As String = " slsdfsdf    "
s = s.Trim

You can add these two subs to implement LTrim and RTrim:
B4X:
Sub LTrim(s As String) As String
   Dim m As Matcher = Regex.Matcher("^(\s+)", s)
   If m.Find Then
     Return s.SubString(m.GetEnd(1))
   Else
     Return s
   End If
End Sub

Sub RTrim(s As String) As String
   Dim m As Matcher = Regex.Matcher("(\s+)$", s)
   If m.Find Then
     Return s.SubString(m.GetEnd(1))
   Else
     Return s
   End If
End Sub
 

MikeH

Well-Known Member
Licensed User
Longtime User
TrimStart:
B4X:
dim count as int

do while Mystring.startswith(" ")

Mystring = Mystring.substring2(count, string.length)
count = count + 1

loop


TrimEnd:
B4X:
dim count as int = Mystring.length

do while Mystring.endswith(" ")

Mystring = Mystring.substring(count)
count = count -1

loop
 
Last edited:
Top