fbpx
Wikipedia

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 ' 

would be changed, after trimming, to

'this is a test' 

Variants edit

Left or right trimming edit

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.[1]

Whitespace character list parameterization edit

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 edit

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 edit

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 edit

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 edit

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,[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 edit

The Braille Patterns Unicode block contains U+2800 BRAILLE PATTERN BLANK, a Braille pattern with no dots raised. The Unicode standard explicitly states that it does not act as a space.

The Non-breaking space U+00A0   NO-BREAK SPACE ( ,  ) can also be treated as non-space for trimming purposes.

Usage edit

References edit

  1. ^ "Trim". Freepascal.org. 2013-02-02. Retrieved 2013-08-24.
  2. ^ "Character (Java 2 Platform SE 5.0)". Java.sun.com. Retrieved 2013-08-24.

External links edit

  • Tcl: string trim
  • Faster JavaScript Trim - compares various JavaScript trim implementations
  • php string cut and trimming- php string cut and trimming

trimming, computer, programming, this, article, needs, additional, citations, verification, please, help, improve, this, article, adding, citations, reliable, sources, unsourced, material, challenged, removed, find, sources, trimming, computer, programming, ne. This article needs additional citations for verification 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 template message 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 would be changed after trimming to this is a test Contents 1 Variants 1 1 Left or right trimming 1 2 Whitespace character list parameterization 1 3 Special empty string return value 1 4 Space normalization 1 5 In place trimming 2 Definition of whitespace 2 1 Non space blanks 3 Usage 4 References 5 External linksVariants editLeft or right trimming edit 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 1 Whitespace character list parameterization edit 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 edit 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 edit 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 edit 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 editThe 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 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 edit The Braille Patterns Unicode block contains U 2800 BRAILLE PATTERN BLANK a Braille pattern with no dots raised The Unicode standard explicitly states that it does not act as a space The Non breaking space U 00A0 NO BREAK SPACE amp nbsp amp NonBreakingSpace can also be treated as non space for trimming purposes Usage editMain article Comparison of programming languages string functions trimReferences edit Trim Freepascal org 2013 02 02 Retrieved 2013 08 24 Character Java 2 Platform SE 5 0 Java sun com Retrieved 2013 08 24 External links editTcl string trim Faster JavaScript Trim compares various JavaScript trim implementations php string cut and trimming php string cut and trimming Retrieved from https en wikipedia org w index php title Trimming computer programming amp oldid 1188107512, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.