Trim$ ( string$ )
Parameters
| string - the string to trim |
Description
|
Returns a copy of the string with whitespace removed from both ends. Trim( " hi " ) gives "hi". Only the ends are touched - spaces inside the string are left alone, so "a b" survives intact. It strips more than spaces. Anything with a character code of 32 or below goes, which covers tabs, carriage returns and line feeds, and the delete character at 127 goes too. That is exactly what you need after reading a line from a text file, where an invisible carriage return on the end would otherwise break every comparison you make. Trim belongs on almost every piece of text that came from outside your program: a name typed by the player, a value read from a config file, a token split out of a longer line. Comparing untrimmed text is one of the most common sources of "but it looks identical" bugs. Pair it with Lower when you want a comparison that forgives both stray spaces and capitalisation. See also: Lower, Upper, Len, Replace, ReadLine. |
Example
; Trim Example ; ------------ ; Trim$ removes the spaces from both ends of a string. typed$=" Robo " Print "A player typed their name as: '"+typed$+"'" Print "Trim$ cleans it up to: '"+Trim$(typed$)+"'" Print "" ; Great for reading back fixed-width save-file fields padded$=LSet$("Zelda",16) Print "Padded save field: '"+padded$+"'" Print "Trimmed back: '"+Trim$(padded$)+"'" ; Spaces in the middle are kept Print "" Print "Trim$(' brave knight ') = '"+Trim$(" brave knight ")+"'" Print "" Print "Press any key to close the example" WaitKey End
Index