Code archives/Algorithms/Functions to trim whitespace from the left or right of a string

This code has been declared by its author to be Public Domain code.

Download source code

Functions to trim whitespace from the left or right of a string by Zethrax2007
RightTrim() and LeftTrim() functions to trim whitespace from the right or left of a string.

The built in BlizMax Trim() method removes the whitespace from both sides, which may be undesirable.
Print ">" + RightTrim( "   abc   " ) + "<"
Print ">" + LeftTrim( "   abc   " ) + "<"

End

Function RightTrim:String( value:String )
	For Local pos:Int = value.length - 1 To 0 Step -1
		If Asc( value[ pos..pos + 1 ] ) > 32
			value = value[ ..pos + 1 ]
			Exit
		EndIf
	Next
	Return value
End Function


Function LeftTrim:String( value:String )
	For Local pos:Int = 0 To value.length - 1
		If Asc( value[ pos..pos + 1 ] ) > 32
			value = value[ pos.. ]
			Exit
		EndIf
	Next
	Return value
End Function

Comments

None.

Code Archives Forum