Trimming (computer programming): Difference between revisions

Content deleted Content added
Ruud Koot (talk | contribs)
Chico75 (talk | contribs)
Links transformed in references
Line 12:
 
==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 [[Object Pascal]] (Delphi) has '''TrimLeft''' and '''TrimRight''' functions[<ref>http://www.freepascal.org/docs-html/rtl/sysutils/trim.html]</ref>.
 
''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 <code>string-trim</code> 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 (computer programming)|predicate]] parameter (a [[functor]]) to select which characters are trimmed.''
Line 25:
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 [<ref>http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#isWhitespace(char) isWhitespace()]</ref> method recognizes Unicode space characters.
 
Delphi's Trim function considers characters U+0000 (NULL) through U+0020 (SPACE) to be whitespace.
Line 59:
|-
|Trim(''String'')
|[[Pascal (programming language)|Pascal]] [<ref>http://gnu-pascal.de/gpc-hr/Trim.html]</ref>, [[QBasic]], [[Visual Basic]], [[Delphi programming language|Delphi]]
|-
|''string''.strip()
Line 131:
 
====C/C++====
There is no standard trim function in C or C++. Most of the available string libraries[<ref>http://www.and.org/vstr/comparison]</ref> 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 software|open source]] C++ library [[Boost library|Boost]] has several trim variants, including a standard one: [<ref>http://www.boost.org/doc/html/string_algo/usage.html#id2742817]</ref>:
 
<source lang="cpp">
Line 140:
</source>
 
Note that with boost's function named simply <code>trim</code> the input sequence is modified in-place[<ref>http://www.boost.org/doc/html/trim.html]</ref>, and does not return a result.
 
Another [[open-source software|open source]] C++ library [[Qt (toolkit)|Qt]] has several trim variants, including a standard one: [<ref>http://doc.trolltech.com/4.5/qstring.html#trimmed]</ref>
 
<source lang="cpp">
Line 149:
</source>
 
The [[Linux kernel]] also includes a strip function, <code>strstrip()</code>, since 2.6.18-rc1, which trims the string "in place". Since 2.6.33-rc1, the kernel uses <code>strim()</code> instead of <code>strstrip()</code> to avoid false warnings. [<ref>http://www.kernel.org/pub/linux/kernel/v2.6/snapshots/patch-2.6.33-rc1-git1.log]</ref>
 
====Haskell====
Line 241:
*[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
 
==Notes==
{{reflist}}
 
[[Category:Articles with example code]]