Talk:Trimming (computer programming)
C/C++ example needs actual code
The C/C++ example isn't too illustrative of the theory behind trimming a string in C. Perhaps an example that contains the actual code would be more appropriate. (I want to see some loops in there!) --indil 20:55, 19 July 2006 (UTC)
- I went for a brief overview and stuck with the simplest functions/libraries/implementations I could find. Please feel free to add some code :) —Pengo talk · contribs 13:02, 23 July 2006 (UTC)
- I've changed the code for the C example. I believe the old example would have leaked memory - for processing large amounts of data, this could cause problems! On a side note, could someone check to make sure I haven't done something dumb while writing this (it compiled and worked in VC++) --Portej 07:29, 26 September 2007 (UTC)
in AWK better to use those command:
function ltrim(s) { sub(/^ */, "", s); return s }
function rtrim(s) { sub(/ *$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
Keep the functions. Remove just code
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 this.
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 uses regular expressions to trim[1] :
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 C++ library Boost has several trim variants, including a standard one: [2]
trimmed = boost::algorithm::trim_copy(string);
Note that with boost's function named simply trim
the input sequence is modified in-place[3], 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".
The open source and portable C and C++ 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:
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;
}
Haskell
A trim algorithm in Haskell[4]
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 [5]:
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 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:
- chop removes the last character from a string and returns it.
- 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: 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
XSLT has the function normalize-space(string)
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.