Variants
- Left or right trimming
- The most popular variants of the trim function strip only the beginning or end of the string. Typically named ltrim and rtrim respectively, or in the case of Python: lstrip and rstrip. C# uses TrimStart and TrimEnd, and Common Lisp string-left-trim and string-right-trim. Pascal and Java do not have these variants built-in, although Object Pascal (Delphi) has TrimLeft and TrimRight functions.
- Whitespace character list parameterization
- Many trim functions have an optional parameter to specify a list of characters to trim, instead of the default whitespace characters. For example, PHP and Python allow this optional parameter, while Pascal and Java do not. With Common Lisp's
string-trim
function, the parameter (called character-bag) is required. The C++ Boost library defines space characters according to locale, as well as offering variants with a predicate parameter (a functor) to select which characters are trimmed.
- Special empty string return value
- An uncommon variant of trim returns a special result if no characters remain after the trim operation. For example, Apache Jakarta's StringUtils has a function called
stripToNull
which returnsnull
in place of an empty string.
- Space normalization
- Space normalization is a related string manipulation where in addition to removing surrounding whitespace, any sequence of whitespace characters within the string is replaced with a single space. Space normalization is performed by the function named
Trim
in spreadsheet applications (including Excel, Calc, Gnumeric, and Google Docs), and by thenormalize-space
function in XSLT and XPath,
- In-place trimming
- While most algorithms return a new (trimmed) string, some alter the original string in-place. Notably, the Boost library allows either in-place trimming or a trimmed copy to be returned.
Read more about this topic: Trimming (computer Programming)
Famous quotes containing the word variants:
“Nationalist pride, like other variants of pride, can be a substitute for self-respect.”
—Eric Hoffer (19021983)