# Trimming (computer programming)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Trimming_(computer_programming)
> Markdown URL: https://mediated.wiki/source/Trimming_(computer_programming).md
> Source: https://en.wikipedia.org/wiki/Trimming_(computer_programming)
> Source revision: 1323393641
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

This article needs more citations. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Trimming" computer programming – news · newspapers · books · scholar · JSTOR (February 2015) (Learn how and when to remove this message)

In [computer programming](/source/Computer_programming), **trimming** (**trim**) or **stripping** (**strip**) is a [string manipulation](/source/String_(computer_science)) in which leading and trailing [whitespace](/source/Whitespace_character) is removed from a [string](/source/String_(computer_science)).

For example, the string (enclosed by apostrophes)

'  this is a test  '

after trimming changes 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](/source/Object_Pascal) (Delphi) has **TrimLeft** and **TrimRight** functions.[1]

### 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](/source/Boost_library) defines space characters according to [locale](/source/Locale_(computer_software)), as well as offering variants with a [predicate](/source/Predicate_(computer_programming)) parameter (a [functor](/source/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](/source/Jakarta_Project)'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](/source/Microsoft_Excel), [Calc](/source/OpenOffice.org_Calc), [Gnumeric](/source/Gnumeric), and [Google Docs](/source/Google_Docs)), and by the normalize-space() function in [XSLT](/source/XSL_Transformations) and [XPath](/source/XPath),

### In-place trimming

While most algorithms return a new (trimmed) string, some alter the original string [in-place](/source/In-place). Notably, the [Boost library](/source/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](/source/Unicode) typically include all Unicode space characters. Some implementations also include [ASCII](/source/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,[2] 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](/source/Braille_Patterns) Unicode block contains U+2800 ⠀ BRAILLE PATTERN BLANK, a [Braille](/source/Braille) pattern with no dots raised. The Unicode standard explicitly states that it does not act as a space.

The [Non-breaking space](/source/Non-breaking_space) U+00A0 NO-BREAK SPACE (&nbsp;, &NonBreakingSpace;) can also be treated as non-space for trimming purposes.

## Usage

Main article: [Comparison of programming languages (string functions) § trim](/source/Comparison_of_programming_languages_(string_functions)#trim)

## References

1. **[^](#cite_ref-1)** ["Trim"](https://www.freepascal.org/docs-html/rtl/sysutils/trim.html). Freepascal.org. 2013-02-02. Retrieved 2013-08-24.

1. **[^](#cite_ref-2)** ["Character (Java 2 Platform SE 5.0)"](https://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#isWhitespace(char)). Java.sun.com. Retrieved 2013-08-24.

## External links

- [Tcl: string trim](https://www.tcl.tk/man/tcl8.4/TclCmd/string.htm#M46)

- [Faster JavaScript Trim](https://blog.stevenlevithan.com/archives/faster-trim-javascript) - compares various JavaScript trim implementations

- [php string cut and trimming](http://webwidetutor.com/php/PHP-Change-String-value-behaviour-or-look-?id=8)- php string cut and trimming

---
Adapted from the Wikipedia article [Trimming (computer programming)](https://en.wikipedia.org/wiki/Trimming_(computer_programming)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Trimming_(computer_programming)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
