The open source C++ library Boost has several trim variants, including a standard one: #include <boost/algorithm/string/trim.hpp> trimmed = boost::algorithm::trim_copy("string"); Note that with boost's function named simply trim the input sequence is modified in-place, and does not return a result. Another open source C++ library Qt has several trim variants, including a standard one: #include <QString> trimmed = s.trimmed(); The Linux kernel also includes a strip function, strstrip(), since 2.6.18-rc1, which trims the string "in place". Since 2.6.33-rc1, the kernel uses strim() instead of strstrip() to avoid false warnings. 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. J The trim algorithm in J is a functional description: trim =. #~ [: (+./\ *. +./\.) ' '&~: That is: filter (#~) for non-space characters (' '&~:) between leading (+./\) and (*.) trailing (+./\.) spaces. JavaScript There is a built-in trim function in JavaScript 1.8.1 (Firefox 3.5 and later), and the ECMAScript 5 standard. In earlier versions it can be added to the String object's prototype as follows: String.prototype.trim = function() { return this.replace(/^\s+/g, "").replace(/\s+$/g, ""); }; Perl Perl 5 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: chop removes the last character from a string and returns it. chomp removes the trailing newline character(s) from a string if present. (What constitutes a newline is $INPUT_RECORD_SEPARATOR dependent). In Raku, the upcoming sister language of Perl, strings have a trim method. Example: $string = $string.trim; # remove leading and trailing whitespace $string .= trim; # same thing 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