Content deleted Content added
removed Category:Algorithms on strings using HotCat |
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
''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
Delphi's Trim function considers characters U+0000 (NULL) through U+0020 (SPACE) to be whitespace.
Line 59:
|-
|Trim(''String'')
|[[Pascal (programming language)|Pascal]]
|-
|''string''.strip()
Line 131:
====C/C++====
There is no standard trim function in C or C++. Most of the available string libraries
The [[open-source software|open source]] C++ library [[Boost library|Boost]] has several trim variants, including a standard one
<source lang="cpp">
Line 140:
</source>
Note that with boost's function named simply <code>trim</code> the input sequence is modified in-place
Another [[open-source software|open source]] C++ library [[Qt (toolkit)|Qt]] has several trim variants, including a standard one:
<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.
====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]]
|