Returns the specified string with extra white spaces removed.
Each sequence of the of contiguous whitespace will be "collapsed",
that is reduced to one or two characters according to the following
rules:
-
If the sequence contains no newline characters ('\n'),
it is replaced with a single space.
-
If the sequence includes only one newline, it is replaced with
a single newline character.
-
If the sequence includes more than one newlines,
it is replaced with two newline characters.
You may use this function to "tidy up" a text, so as when it is printed,
(1) all words will be separated with only one space,
(2) each line never starts or ends with a space,
(3) the lines with the text are separated with no more than one empty line.
Parameters:
s
-
The source string
trim
-
Specifies whether to remove both leading and trailing white space.
When false
, the leading and trailing white space will not be
removed completely but rather collapsed (as explained above).
trimLeft
-
Specifies whether to remove leading white space.
When false
, the leading white space will not be
removed completely but rather collapsed (as explained above).
trimRight
-
Specifies whether to remove trailing white space (default
true
).
When false
, the trailing white space will not be
removed completely but rather collapsed (as explained above).
Note:
-
When no trim parameters are specified, by default both
left and right trimmings will be assumed as
true
. That is,
collapseSpaces (s)
is the same as the call
collapseSpaces (s, true)