### 5.2.5 Finding and removing leading/trailing whitespace

The trim command can find and/or remove leading and trailing whitespace in a string (see Section 21.2.2, Section 28.1.9 and Section 28.2.2 for other usages of trim).

- trim takes one mandatory arguments and one or two optional arguments:
  - str, a string.
  - Optionally, left or right, the symbol specifying an one-sided truncation.
  - Optionally, index, the symbol.
- trim(str⟨,left|right⟩  ⟨,index⟩) finds the index l of the first non-whitespace character in str and the index u of the first trailing whitespace character in str. If index is given, then the return value is either l if left is given, u if right is given, or (l,u−l) otherwise (the last sequence contains the start and length of the truncated string). If index is omitted, then the return value is the portion between the lth character (inclusive) and uth character (exclusive); if left is given, then u=length(str), and if right is given, then l=0.
- Whitespace characters in ASCII are: 9 (horizontal tab), 10 (line feed), 11 (vertical tab), 12 (form feed), 13 (carriage return) and 32 (space).

Instead of trim, you can also use the strip command to remove whitespace, but note that it discards only space characters.

#### Examples

trim(" this is a string\n\t ")

“this is a string” |  |  |  |  |  |  |  |  |  |

trim(" this is a string\n\t ",right)

“ this is a string” |  |  |  |  |  |  |  |  |  |

trim(" 12345 ",index)

2,5 |  |  |  |  |  |  |  |  |  |