Talk:Trimming (computer programming): Difference between revisions

Content deleted Content added
Line 26:
Hi, I think you should remove the "other languages which don't have trim functions" section. They '''don't''' have trim or a close analogue, so they should be trimmed. It is superfluous text at the end of the article that happens to take up more than half the article that is supposed to be on '''trim''' rather than what is not trim. It is quite easy to define trim-like and, by omision what is not trim-like. That section becomes a tutorial on how to write trim in other languages and should be removed or moved to this talk page. Please refer to [[What_Wikipedia_is_not#Wikipedia_is_not_a_manual.2C_guidebook.2C_or_textbook| this]]. --[[User:Paddy3118|Paddy]] 05:32, 10 October 2007 (UTC)
 
:It's about trim library functions as well as trim algorithms. Most of the examples (except C) are only one or two lines of code (which is probably why they don't have library functions). Hardly a hand-holding instruction manual. Leaving them out is to pretend trimming isn't used in these languages, and removes much of the language-specific discussion. Lastly, the code certainly doesn't belong on the talk page. —[[User:Pengo|Pengo]] 14:26, 10 October 2007 (UTC)
===Trim in other languages===
In languages without a built-in trim function, a custom function may need to be written, or a library found.
 
====AWK====
 
[[AWK programming language|AWK]] uses regular expressions to trim[http://www.faqs.org/qa/qa-1120.html] :
 
ltrim(v) = gsub(/^[ \t]+/, "", v)
rtrim(v) = gsub(/[ \t]+$/, "", 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++. The equivalent function has also often been called '''EatWhitespace''' in non-standard C libraries.
 
The [[open-source software|open source]] C++ library [[Boost library|Boost]] has several trim variants, including a standard one: [http://www.boost.org/doc/html/string_algo/usage.html#id2742817]
 
trimmed = boost::algorithm::trim_copy(''string'');
 
Note that with boost's function named simply <code>trim</code> 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, <code>strstrip()</code>, since 2.6.18-rc1, which trims the string "in place".
 
The [[open-source software|open source]] and [[Porting|portable]] C and C++ library [[Better String Library|The Better String Library]] has support for trimming as well:
 
btrimws (b = bfromcstr (" string "));
 
The following C code snippet can be used to remove white spaces from a string:
 
<source lang="c">
char* trimWhitespace(char* inString);
int isWhitespace(char c);
 
int
isWhitespace(char c)
{
return c == ' ' || c == '\t' || c == '\v' || c == '\f';
}
 
char*
trimWhitespace(char* iS)
{
char *ret = 0;
int iSLength = 0, retLength = 0, sStart = 0, sEnd = 0;
int i = 0, n = 0;
 
iSLength = (int)strlen(iS);
 
// Find first non-whitespace character
while (sStart < iSLength && isWhitespace(iS[sStart]))
sStart++;
 
// Find last character before all whitespace
// Starting point is the last character
sEnd = iSLength - 1;
while(sEnd > 0 && isWhitespace(iS[sEnd]))
sEnd--;
 
// Create a new string and allocate the memory
retLength = (sEnd-sStart + 1) + 1; // Extra 1 is for the NULL terminator
ret = (char*)malloc(retLength * sizeof(char));
for (i = sStart; i <= sEnd; i++)
ret[n++] = iS[i];
ret[n] = `\0`;
 
return ret;
}
</source>
 
====Haskell====
 
A trim algorithm in [[Haskell (programming language)|Haskell]][http://www.haskell.org/haskellwiki/Simple_unix_tools]
 
import Data.Char
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 class [http://jibbering.com/faq/#FAQ4_16]:
 
To add a trim function to all strings:
String.prototype.trim = function() {
return this.replace(/^\s*|\s*$/g, "")
}
 
This allows the same syntax as Java to be used for JavaScript.
 
====Perl====
Perl has no built-in function, and a trimming is usually achieved through [[regular expression]]s.
 
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 <code>$string</code>.
 
Also available for Perl is '''StripLTSpace''' in <code>String::Strip</code> from [[CPAN]].
 
There are however two functions that are commonly used to strip whitespace from the end of strings, chomp and chop:
* chop removes the last character from a string and returns it.
* chomp removes the trailing newline from a string if present.
 
====Tcl====
The [[Tcl]] <code>string</code> command has three relevant subcommands: <code>trim</code>, <code>trimright</code> and <code>trimleft</code>. 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: trimming vowels
set string onomatopoeia
set trimmed [string trim $string aeiou] ;# result is nomatop
set r_trimmed [string trimright $string aeiou] ;# result is onomatop
set l_trimmed [string trimleft $string aeiou] ;# result is nomatopoeia
 
====XSLT====
[[XSL Transformations|XSLT]] has the function <code>normalize-space(''string'')</code> which strips leading and trailing whitespace and also replaces any sequence of whitespace characters (including linebreaks) with a single space.
 
Example:
<xsl:variable name='trimmed'>
<xsl:value-of select='normalize-space(''string'')'/>
</xsl:variable>
 
XSLT 2.0 also includes regular expressions, providing another mechanism to perform trimming.