In computer programming, trimming (trim) or stripping (strip) is a string manipulation in which leading and trailing whitespace is removed from a string.
Contents
For example, the string (enclosed by apostrophes)
would be changed, after trimming, to
Variants
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.stripToNull
which returns null
in place of an empty string.Trim()
in spreadsheet applications (including Excel, Calc, Gnumeric, and Google Docs), and by the normalize-space()
function in XSLT and XPath,Definition of whitespace
The characters which are considered whitespace varies between programming languages and implementations. For example, C traditionally only counts space, tab, line feed, and carriage return characters, while languages which support Unicode typically include all Unicode space characters. Some implementations also include ASCII control codes (non-printing characters) along with whitespace characters.
Java's trim method considers ASCII spaces and control codes as whitespace, contrasting with the Java isWhitespace()
method, which recognizes all Unicode space characters.
Delphi's Trim function considers characters U+0000 (NULL) through U+0020 (SPACE) to be whitespace.
Non-space blanks
The Braille Patterns Unicode block contains U+2800 ⠀ Braille pattern blank (HTML ⠀
), a Braille pattern with no dots raised. The Unicode standard explicitly states that it does not act as a space.