Functions are available to remove leading, trailing, or both spaces from strings.
[Excel VBA]
Trim(str): Removes leading and trailing spaces.
LTrim(str): Removes leading spaces.
RTrim(str): Removes trailing spaces.
Sample file: Samples\ch06\Excel VBA\TrimSpaces.xlsm.
code.vba
Sub Test()
Dim strA As String
strA = " ab cd "
Debug.Print Trim(strA) ' "ab cd"(both ends trimmed)
Debug.Print LTrim(strA) ' "ab cd "(leading trimmed)
Debug.Print RTrim(strA) ' " ab cd"(trailing trimmed)
End Sub
[Python]
strip(): Removes leading and trailing spaces.
lstrip(): Removes leading spaces.
rstrip(): Removes trailing spaces.
code.python
>>> ' ab cd '.strip() # 'ab cd' (both ends)
>>> ' ab cd '.lstrip() # 'ab cd ' (leading)
>>> ' ab cd '.rstrip() # ' ab cd' (trailing)