Trim (programming)

Trim (programming)

In programming, trim or strip is a common string manipulation function which removes leading and trailing whitespace from a string.

For example, in Python:

' this is a test '.strip()

returns the string:

'this is a test'

Variants

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 Delphi (Borland's object-oriented derivative of Pascal) has TrimLeft and TrimRight functions [http://www.freepascal.org/docs-html/rtl/sysutils/trim.html] .

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.

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.

An alternative to trimming a string is space normalization, where in addition to removing surrounding whitespace, any sequence of whitespace characters within the string is replaced with a single space. Space normalization is done by Trim() in spreadsheet applications (including Excel, Calc, Gnumeric, and Google Docs), and by the normalize-space() function in XSLT and XPath,

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, while Java's [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#isWhitespace(char) isWhitespace()] method recognizes Unicode space characters.

Usage

Following are examples of trimming a string using several programming languages. All of the implementations shown return a new string and do not alter the original variable.

Other languages

In languages without a built-in trim function, it is usually simple to create a custom function which accomplishes the same task.

AWK

In AWK, one can use regular expressions to trim:

ltrim(v) = gsub(/^ [ ] +/, "", v) rtrim(v) = gsub(/ [ ] +$/, "", v) trim(v) = ltrim(v); rtrim(v)

or:

function ltrim(s) { sub(/^ +/, "", s); return s } function rtrim(s) { sub(/ +$/, "", s); return s } function trim(s) { return rtrim(ltrim(s)); }

C/C++

There is no standard trim function in C or C++. Most of the available string libraries [http://www.and.org/vstr/comparison] for C contain code which implements trimming, or functions that significantly ease an efficient implementation. The function has also often been called EatWhitespace in some, non-standard C libraries.

The open source C++ library Boost has several trim variants, including a standard one: [http://www.boost.org/doc/html/string_algo/usage.html#id2742817]


#include trimmed = boost::algorithm::trim_copy("string");

Note that with boost's function named simply trim the input sequence is modified in-place [http://www.boost.org/doc/html/trim.html] , and does not return a result.

The Linux kernel also includes a strip function, strstrip(), since 2.6.18-rc1, which trims the string "in place".

Haskell

A trim algorithm in Haskell:

import Data.Char (isSpace) trim :: String -> String trim = f . f where f = reverse . dropWhile isSpace

may be interpreted as follows: "f" drops the preceding whitespace, and reverses the string. "f" is then again applied to its own output. Note that the type signature (the second line) is optional.

JavaScript

There is no built-in trim function, but it can be added to the String object's prototype to add a trim method to all strings:

String.prototype.trim = function() { return this.replace(/^s+|s+$/g, "");}

Perl

Perl has no built-in trim function. However, the functionality is commonly achieved using regular expressions.

Example: $string =~ s/^s+//; # remove leading whitespace$string =~ s/s+$//; # remove trailing whitespace or: $string =~ s/^s+|s+$//g ; # remove both leading and trailing whitespace These examples modify the value of the original variable $string.

Also available for Perl is StripLTSpace in String::Strip from CPAN.

There are however two functions that are commonly used to strip whitespace from the end of strings, chomp and chop:
* [http://perldoc.perl.org/functions/chop.html chop] removes the last character from a string and returns it.
* [http://perldoc.perl.org/functions/chomp.html chomp] removes the trailing newline from a string if present.

Tcl

The Tcl string command has three relevant subcommands: trim, trimright and trimleft. For each of those commands, an additional argument may be specified: a string that represents a set of characters to remove -- the default is whitespace (space, tab, newline, carriage return).

Example of trimming vowels:

set string onomatopoeiaset trimmed [string trim $string aeiou] ;# result is nomatopset r_trimmed [string trimright $string aeiou] ;# result is onomatopset l_trimmed [string trimleft $string aeiou] ;# result is nomatopoeia

XSLT

XSLT includes the function normalize-space("string") which strips leading and trailing whitespace, in addition to replacing any whitespace sequence (including line breaks) with a single space.

Example: XSLT 2.0 includes regular expressions, providing another mechanism to perform string trimming.

Another XSLT technique for trimming is to utilize the XPath 2.0 substring() function.

ee also

*String functions (programming)

External links

* [http://www.tcl.tk/man/tcl8.4/TclCmd/string.htm#M46 Tcl: string trim]
* [http://blog.stevenlevithan.com/archives/faster-trim-javascript Faster JavaScript Trim] - compares various JavaScript trim implementations


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Trim — may refer to:Places* Trim, County Meath, a town and castle in Ireland * Trim Station (OC Transpo), a bus station in Ottawa, Canada * Trim Road, in Ottawa, CanadaDecoration* Trim (sewing), ornaments applied to clothing or other textiles * Trim… …   Wikipedia

  • Comparison of programming languages (string functions) — String functions redirects here. For string functions in formal language theory, see String operations. Programming language comparisons General comparison Basic syntax Basic instructions Arrays …   Wikipedia

  • Whitespace (computer science) — In computer science, whitespace is any single character or series of characters that represents horizontal or vertical space in typography. When rendered, a whitespace character does not correspond to a visual mark, but typically does occupy an… …   Wikipedia

  • Longest common subsequence problem — Not to be confused with longest common substring problem. The longest common subsequence (LCS) problem is to find the longest subsequence common to all sequences in a set of sequences (often just two). Note that subsequence is different from a… …   Wikipedia

  • Everything That Happens Will Happen Today — Everything That Happens Will Happen Today …   Wikipedia

  • Buick Regal — Infobox Automobile name = Buick Regal manufacturer = Buick parent company = General Motors class = Personal luxury car (1973 1979) Mid size (1980 2004) production = 1973–2004 successor = Buick LaCrosse The Buick Regal is a mid size car produced… …   Wikipedia

  • Wikipedia:Reference desk/Computing — The Wikipedia Reference Desk covering the topic of computing. Computing #eee #f5f5f5 #eee #aaa #aaa #aaa #00f #36b #000 #00f computing Wikipedia:Reference de …   Wikipedia

  • List of Red vs. Blue characters — This is a list of characters in the Rooster Teeth series Red vs. Blue. Contents 1 Red Team 1.1 Sarge 1.2 Simmons 1.3 Grif 1.4 …   Wikipedia

  • Felipe Delgado — This article is about an American record producer. For an Ecuadorian swimmer, see Felipe Delgado (swimmer). For Felix Delgado, a Cuban rapper, see Cuban Link. Felipe Delgado Background information …   Wikipedia

  • Media and Publishing — ▪ 2007 Introduction The Frankfurt Book Fair enjoyed a record number of exhibitors, and the distribution of free newspapers surged. TV broadcasters experimented with ways of engaging their audience via the Internet; mobile TV grew; magazine… …   Universalium

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”