### Trim, Pad and Fill String

Examples to Trim, Pad and Fill a String.
'Remove all left blanks from a string
`Sub TrimLeft(s As String) As String
Do While s.StartsWith(" ") = True
If s.StartsWith(" ") = True Then s = s.SubString(1)
Loop
Return s
End Sub`
'Remove all right blanks from a string
`Sub TrimRight(s As String) As String
Do While s.EndsWith(" ") = True
If s.EndsWith(" ") = True Then s = s.SubString2(0, s.Length - 1)
Loop
Return s
End Sub`
'Pad a string left with char given
`Sub PadLeft(s As String, n As Int, ch As String) As String
Dim st As String = ""
For i = 1 To n
st = st & ch
Next
Return st & s
End Sub`
'Pad a string right with char given
`Sub PadRight(s As String, n As Int, ch As String) As String
Dim st As String = ""
For i = 1 To n
st = st & ch
Next
Return s & st
End Sub`
'Fill a string n times with char given
`Sub FillStr(n As Int, ch As String) As String
Dim s As String = ""
For i = 1 To n
s = s & ch
Next
Return s
End Sub`
Test
`Sub buttonTest_Action
Dim s As String = " Hello World "
Log("String:[" & s & "]")
Log("String Trim:[" & s.Trim & "]")
Log("String TrimLeft:[" & TrimLeft(s) & "]")
Log("String TrimRight:[" & TrimRight(s) & "]")
Log("********************************")
Dim s As String = "Hello World"
Log("String:[" & s & "]")
Log("String PadLeft:[" & PadLeft(s, 10, "*") & "]")
Log("String PadRight:[" & PadRight(s, 10, "*") & "]")
Log("String FillStr:[" & FillStr(5, "*") & "]")
End Sub`
Output
String:[ Hello World ]
String Trim:[Hello World]
String TrimLeft:[Hello World ]
String TrimRight:[ Hello World]
********************************
String:[Hello World]
String PadLeft:[**********Hello World]
String PadRight:[Hello World**********]
String FillStr:[*****]

Changed: , Category: Strings, File: T6C255M1862.htm, ID: 1862