In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, trimming (trim) or stripping (strip) is a
string manipulation in which leading and trailing
whitespace is removed from a
string.
For example, the string (enclosed by apostrophes)
' this is a test '
would be changed, after trimming, to
'this is a test'
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
Object Pascal is an extension to the programming language Pascal (programming language), Pascal that provides object-oriented programming (OOP) features such as Class (computer programming), classes and Method (computer programming), methods.
T ...
(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
In mathematics, specifically category theory, a functor is a Map (mathematics), mapping between Category (mathematics), categories. Functors were first considered in algebraic topology, where algebraic objects (such as the fundamental group) ar ...
) 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 returns
null
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 the
normalize-space()
function in
XSLT and
XPath
XPath (XML Path Language) is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) in 1999, and can be used to compute values (e.g., strings, numbers, or ...
,
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.
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
Unicode or ''The Unicode Standard'' or TUS is a character encoding standard maintained by the Unicode Consortium designed to support the use of text in all of the world's writing systems that can be digitized. Version 16.0 defines 154,998 Char ...
typically include all Unicode space characters. Some implementations also include
ASCII
ASCII ( ), an acronym for American Standard Code for Information Interchange, is a character encoding standard for representing a particular set of 95 (English language focused) printable character, printable and 33 control character, control c ...
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 , a
Braille
Braille ( , ) is a Tactile alphabet, tactile writing system used by blindness, blind or visually impaired people. It can be read either on embossed paper or by using refreshable braille displays that connect to computers and smartphone device ...
pattern with no dots raised.
The Unicode standard explicitly states that it does not act as a space.
The
Non-breaking space can also be treated as non-space for trimming purposes.
Usage
References
{{Reflist
External links
Tcl: string trimFaster JavaScript Trim- compares various JavaScript trim implementations
php string cut and trimming php string cut and trimming
Articles with example code
String (computer science)