- Source:
Methods
trim(subject, charactersopt, fromopt, nullable) → {String}
Removes whitespace or characters from the beginning and the end (or just one side) of a string.
This is usually used to sanitize values and remove leading or trailing whitespace in user input.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
subject |
String | the string to trim |
||
characters |
String | Array.<String> |
<optional> |
null | defines which character(s) to trim; will trim whitespace if nullish (or '\s'); if you want to trim whitespace and something else, define an array including a nullish value (or '\s') as well as the other characters; this parameter uses regex definitions |
from |
String |
<optional> <nullable> |
'both' | the side(s) from which to trim, either "both", "left" or "right" |
- Source:
Returns:
the trimmed subject
- Type
- String
Example
trim(' foo ')
=> 'foo'
trim(' foo ', null, 'right')
=> ' foo'
trim('abcdefghijklmnopqrstuvwxyz', ['[a-f]', '[u-z]', 'l'], 'both')
=> 'ghijklmnopqrst'