encyclopegia.org

Trimming (computer programming)

In computer programming, 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 ' after trimming changes to 'this is a test'

In computer programming, 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 ' 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 (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 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, ===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 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. ==Usage== ==References== ==External links== Tcl: string trim Faster JavaScript Trim - compares various JavaScript trim implementations php string cut and trimming- php string cut and trimming

References

  1. Trim · retrieved 2013-08-23 · rev 1323393641
    Cite this reference

    MLA "Trim." 2013-08-23. https://www.freepascal.org/docs-html/rtl/sysutils/trim.html. Accessed 24 November 2026.

    Chicago "Trim." 2013-08-23. https://www.freepascal.org/docs-html/rtl/sysutils/trim.html.

    APA encyclopegia.org. (2013). Trim. Retrieved November 24, 2026, from https://www.freepascal.org/docs-html/rtl/sysutils/trim.html

  2. Character (Java 2 Platform SE 5.0) · retrieved 2013-08-23 · rev 1323393641
    Cite this reference

    MLA "Character (Java 2 Platform SE 5.0)." "Character (Java 2 Platform SE 5. 2013-08-23. https://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#isWhitespace(char). Accessed 24 November 2026.

    Chicago "Character (Java 2 Platform SE 5.0)." "Character (Java 2 Platform SE 5. 2013-08-23. https://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#isWhitespace(char).

    APA "Character (Java 2 Platform SE 5. (2013). Character (Java 2 Platform SE 5.0). Retrieved November 24, 2026, from https://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#isWhitespace(char)

Cite this article

MLA

"Trimming (computer programming)." encyclopegia.org, 2025, https://encyclopegia.org/wiki/trimming-computer-programming. Accessed 24 November 2026.

Chicago

"Trimming (computer programming)." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/trimming-computer-programming.

APA

encyclopegia.org. (2025). Trimming (computer programming). Retrieved November 24, 2026, from https://encyclopegia.org/wiki/trimming-computer-programming

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1323393641 · 0.5 · snapshot enwiki-001323393641

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.