Comparison of programming languages (string functions): Difference between revisions

Content deleted Content added
GreenC bot (talk | contribs)
m 1 archive template merged to {{webarchive}} (WAM)
m <source> → <syntaxhighlight>; <pre> for unsupported lang
Line 157:
|}
 
<sourcesyntaxhighlight lang="pascal">
{ Example in Pascal }
var
Line 164:
begin
MyChar := MyStr[2]; // 'e'
</syntaxhighlight>
</source>
 
<pre>
<source lang="algol68">
# Example in ALGOL 68 #
"Hello, World"[2]; // 'e'
</sourcepre>
 
<sourcesyntaxhighlight lang="csharp">
// Example in C# and Ya
"Hello, World"[2]; // 'l'
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="python">
# Examples in Python
"Hello, World"[2] # 'l'
"Hello, World"[-3] # 'r'
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="vbvbnet">
' Example in Visual Basic
Mid("Hello, World",2,1)
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="vbnet">
' Example in Visual Basic .NET
"Hello, World".Chars(2) ' "l"c
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="smalltalk">
" Example in Smalltalk "
'Hello, World' at: 2. "$e"
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 295:
|[[PL/I]]<ref name="lower1">IBM extension.</ref>
|}
<sourcesyntaxhighlight lang="python">
# Example in Python
cmp("hello", "world") # returns -1
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="rexx">
/** Example in Rexx */
compare("hello", "world") /* returns index of mismatch: 1 */
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="scheme">
; Example in Scheme
(use-modules (srfi srfi-13))
; returns index of mismatch: 0
(string-compare "hello" "world" values values values)
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 387:
|}
 
<sourcesyntaxhighlight lang="erlang">
% Example in Erlang
"hello" > "world". % returns false
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="powershell">
# Example in Windows PowerShell
"hello" -gt "world" # returns false
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="lisp">
;; Example in Common Lisp
(string> "art" "painting") ; returns nil
(string< "art" "painting") ; returns non nil
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 478:
|}
 
<sourcesyntaxhighlight lang="pascal">
{ Example in Pascal }
'abc' + 'def'; // returns "abcdef"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="csharp">
// Example in C#
"abc" + "def"; // returns "abcdef"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="vbvbnet">
' Example in Visual Basic
"abc" & "def" ' returns "abcdef"
Line 494:
"abc" & Null ' returns "abc"
"abc" + Null ' returns Null
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="d">
// Example in D
"abc" ~ "def"; // returns "abcdef"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="lisp">
;; Example in common lisp
(concatenate 'string "abc " "def " "ghi") ; returns "abc def ghi"
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 598:
string in string("z", '''loc int''', "word"); ¢ returns '''false''' ¢
 
<sourcesyntaxhighlight lang="csharp">
// Example In C#
"Hello mate".Contains("e"); // returns true
"word".Contains("z"); // returns false
</syntaxhighlight>
</source>
<sourcesyntaxhighlight lang="python">
# Example in Python
"e" in "Hello mate" # returns true
"z" in "word" # returns false
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="smalltalk">
" Example in Smalltalk "
'Hello mate' includesSubstring: 'e' " returns true "
'word' includesSubstring: 'z' " returns false "
</syntaxhighlight>
</source>
 
===Equality===
Line 664:
|}
 
<sourcesyntaxhighlight lang="csharp">
// Example in C#
"hello" == "world" // returns false
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="vbvbnet">
' Example in Visual Basic
"hello" = "world" ' returns false
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="ps1">
# Example in Windows PowerShell
"hello" -eq "world" # returns false
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 837:
|}
 
<sourcesyntaxhighlight lang="lisp">
; Examples in Common Lisp
(search "e" "Hello mate") ; returns 1
(search "z" "word") ; returns NIL
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="csharp">
// Examples in C#
"Hello mate".IndexOf("e"); // returns 1
"Hello mate".IndexOf("e", 4); // returns 9
"word".IndexOf("z"); // returns -1
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="scheme">
; Examples in Scheme
(use-modules (srfi srfi-13))
(string-contains "Hello mate" "e") ; returns 1
(string-contains "word" "z") ; returns #f
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="vbvbnet">
' Examples in Visual Basic
InStr("Hello mate", "e") ' returns 2
InStr(5, "Hello mate", "e") ' returns 10
InStr("word", "z") ' returns 0
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="smalltalk">
" Examples in Smalltalk "
'Hello mate' indexOfSubCollection:'ate' "returns 8"
Line 874:
indexOfSubCollection:'late'
ifAbsent:[ self error ] "raises an exception"
</syntaxhighlight>
</source>
 
<!-- endsection -->
Line 972:
|}
 
<sourcesyntaxhighlight lang="csharp">
// Examples in C#
"Hello mate".IndexOf('e'); // returns 1
"word".IndexOf('z') // returns -1
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="lisp">
; Examples in Common Lisp
(position #\e "Hello mate") ; returns 1
(position #\z "word") ; returns NIL
</syntaxhighlight>
</source>
<!-- endsection -->
{{note|Fortran find|a}} Given a set of characters, SCAN returns the position of the first character found,<ref>{{cite web|url=http://fortranwiki.org/fortran/show/scan |title=scan in Fortran Wiki |publisher=Fortranwiki.org |date=2009-04-30 |accessdate=2013-08-18}}</ref> while VERIFY returns the position of the first character that does not belong to the set.<ref>{{cite web|url=http://fortranwiki.org/fortran/show/verify |title=verify in Fortran Wiki |publisher=Fortranwiki.org |date=2012-05-03 |accessdate=2013-08-18}}</ref>
Line 1,110:
|}
 
<sourcesyntaxhighlight lang="csharp">
// Example in C#
String.Format("My {0} costs {1:C2}", "pen", 19.99); // returns "My pen costs $19.99"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="delphi">
// Example in Object Pascal (Delphi)
Format('My %s costs $%2f', ['pen', 19.99]); // returns "My pen costs $19.99"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="java5java">
// Example in Java
String.format("My %s costs $%2f", "pen", 19.99); // returns "My pen costs $19.99"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="python">
# Example in Python
"My %s costs $%.2f" % ("pen", 19.99); # returns "My pen costs $19.99"
"My {0} costs ${1:.2f}".format("pen", 19.99); # returns "My pen costs $19.99"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="Schemescheme">
; Example in Scheme
(format "My ~a costs $~1,2F" "pen" 19.99) ; returns "My pen costs $19.99"
</syntaxhighlight>
</source>
 
<pre>
<source lang=PLI>
/* example in PL/I */
put string(some_string) edit('My ', 'pen', ' costs', 19.99)(a,a,a,p'$$$V.99')
/* returns "My pen costs $19.99" */
</sourcepre>
<!-- endsection -->
 
Line 1,199:
|}
 
<sourcesyntaxhighlight lang="csharp">
// Example in C#
"hello" != "world" // returns true
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="vbvbnet">
' Example in Visual Basic
"hello" <> "world" ' returns true
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="clojure">
;; Example in Clojure
(not= "hello" "world") ; ⇒ true
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="powershell">
# Example in Windows PowerShell
"hello" -ne "world" # returns true
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 1,320:
|}
 
<sourcesyntaxhighlight lang="csharp">
// Example in C#
String.Join("-", {"a", "b", "c"}) // "a-b-c"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="smalltalk">
" Example in Smalltalk "
#('a' 'b' 'c') joinUsing: '-' " 'a-b-c' "
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="perl">
# Example in Perl
join( '-', ('a', 'b', 'c')); # 'a-b-c'
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="python">
# Example in Python
"-".join(["a", "b", "c"]) # 'a-b-c'
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="ruby">
# Example in Ruby
["a", "b", "c"].join("-") # 'a-b-c'
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="scheme">
; Example in Scheme
(use-modules (srfi srfi-13))
(string-join '("a" "b" "c") "-") ; "a-b-c"
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 1,459:
|}
 
<sourcesyntaxhighlight lang="rexx">
/* Examples in Rexx */
left("abcde", 3) /* returns "abc" */
left("abcde", 8) /* returns "abcde " */
left("abcde", 8, "*") /* returns "abcde***" */
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="scheme">
; Examples in Scheme
(use-modules (srfi srfi-13))
(string-take "abcde", 3) ; returns "abc"
(string-take "abcde", 8) ; error
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="Cppcpp">
' Examples in Visual Basic
Left("sandroguidi", 3) ' returns "san"
Left("sandroguidi", 100) ' returns "sandroguidi"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="Cppcpp">
// Examples in Ya
"abcde"[0..3] // returns "abc"
"abcde"[0..8] // returns "abcde"
</syntaxhighlight>
</source>
 
<!-- endsection -->
Line 1,624:
|}
 
<sourcesyntaxhighlight lang="csharp">
// Examples in C#
"hello".Length; // returns 5
"".Length; // returns 0
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="erlang">
# Examples in Erlang
string:len("hello"). % returns 5
string:len(""). % returns 0
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="perl">
# Examples in Perl
length("hello"); # returns 5
length(""); # returns 0
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="vbvbnet">
' Examples in Visual Basic
Len("hello") ' returns 5
Len("") ' returns 0
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="objc">
//Examples in Objective-C
[@"hello" Length] //returns 5
[@"" Length] //returns 0
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="lua">
-- Examples in Lua
("hello"):len() -- returns 5
#"" -- returns 0
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 1,772:
|}
 
<sourcesyntaxhighlight lang="csharp">
// Example in C#
"Wiki means fast?".ToLower(); // "wiki means fast?"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="scheme">
; Example in Scheme
(use-modules (srfi srfi-13))
(string-downcase "Wiki means fast?") ; "wiki means fast?"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="c">
/* Example in C */
#include <ctype.h>
Line 1,797:
return 0;
}
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 1,828:
|}
 
<sourcesyntaxhighlight lang="python">
# Examples in Python
"Spam eggs spam spam and ham".partition('spam') # ('Spam eggs ', 'spam', ' spam and ham')
"Spam eggs spam spam and ham".partition('X') # ('Spam eggs spam spam and ham', "", "")
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="perl">
# Examples in Perl
split /(spam)/'Spam eggs spam spam and ham',2; # ('Spam eggs ', 'spam', ' spam and ham');
split /(X)/'Spam eggs spam spam and ham',2; # ('Spam eggs spam spam and ham');
</syntaxhighlight>
</source>
 
<!-- endsection -->
Line 1,925:
|}
 
<sourcesyntaxhighlight lang="csharp">
// Examples in C#
"effffff".Replace("f", "jump"); // returns "ejumpjumpjumpjumpjumpjump"
"blah".Replace("z", "y"); // returns "blah"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="java5java">
// Examples in Java
"effffff".replace("f", "jump"); // returns "ejumpjumpjumpjumpjumpjump"
"effffff".replaceAll("f*", "jump"); // returns "ejump"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="vbvbnet">
' Examples in Visual Basic
Replace("effffff", "f", "jump") ' returns "ejumpjumpjumpjumpjumpjump"
Replace("blah", "z", "y") ' returns "blah"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="powershell">
# Examples in Windows PowerShell
"effffff" -replace "f", "jump" # returns "ejumpjumpjumpjumpjumpjump"
"effffff" -replace "f*", "jump" # returns "ejump"
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 2,030:
|}
 
<sourcesyntaxhighlight lang="smalltalk">
" Example in Smalltalk "
'hello' reversed " returns 'olleh' "
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="perl">
# Example in Perl
reverse "hello" # returns "olleh"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="python">
# Example in Python
"hello"[::-1] # returns "olleh"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="scheme">
; Example in Scheme
(use-modules (srfi srfi-13))
(string-reverse "hello") ; returns "olleh"
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 2,153:
|}
 
<sourcesyntaxhighlight lang="lisp">
; Examples in Common Lisp
(search "e" "Hello mate" :from-end t) ; returns 9
(search "z" "word" :from-end t) ; returns NIL
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="csharp">
// Examples in C#
"Hello mate".LastIndexOf("e"); // returns 9
"Hello mate".LastIndexOf("e", 4); // returns 1
"word".LastIndexOf("z"); // returns -1
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="vbvbnet">
' Examples in Visual Basic
InStrRev("Hello mate", "e") ' returns 10
InStrRev(5, "Hello mate", "e") ' returns 2
InStrRev("word", "z") ' returns 0
</syntaxhighlight>
</source>
 
<!-- endsection -->
Line 2,246:
|}
 
<sourcesyntaxhighlight lang="rexx">
/* Examples in Rexx */
right("abcde", 3) /* returns "cde" */
right("abcde", 8) /* returns " abcde" */
right("abcde", 8, "*") /* returns "***abcde" */
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="scheme">
; Examples in Scheme
(use-modules (srfi srfi-13))
(string-take-right "abcde", 3) ; returns "cde"
(string-take-right "abcde", 8) ; error
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="vbvbnet">
' Examples in Visual Basic
Right("sandroguidi", 3) ' returns "idi"
Right("sandroguidi", 100) ' returns "sandroguidi"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="java">
// Examples in Java; extract rightmost 4 characters
String str = "CarDoor";
str.substring(str.length()-4); // returns 'Door'
</syntaxhighlight>
</source>
 
<!-- endsection -->
Line 2,291:
|}
 
<sourcesyntaxhighlight lang="python">
# Examples in Python
"Spam eggs spam spam and ham".rpartition('spam') ### ('Spam eggs spam ', 'spam', ' and ham')
"Spam eggs spam spam and ham".rpartition('X') ### ("", "", 'Spam eggs spam spam and ham')
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 2,371:
|}
 
<sourcesyntaxhighlight lang="csharp">
// Example in C#
"abc,defgh,ijk".Split(','); // {"abc", "defgh", "ijk"}
"abc,defgh;ijk".Split(',', ';'); // {"abc", "defgh", "ijk"}
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="erlang">
% Example in Erlang
string:tokens("abc;defgh;ijk", ";"). % ["abc", "defgh", "ijk"]
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="java">
// Examples in Java
"abc,defgh,ijk".split(","); // {"abc", "defgh", "ijk"}
"abc,defgh;ijk".split(",|;"); // {"abc", "defgh", "ijk"}
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="pascal">
{ Example in Pascal }
var
Line 2,401:
lStr := lStrings.Strings[2]; // 'ijk'
end;
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="perl">
# Examples in Perl
split(/spam/, 'Spam eggs spam spam and ham'); # ('Spam eggs ', ' ', ' and ham')
split(/X/, 'Spam eggs spam spam and ham'); # ('Spam eggs spam spam and ham')
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 2,540:
|}
 
<sourcesyntaxhighlight lang="csharp">
// Examples in C#
"abc".Substring(1, 1): // returns "b"
"abc".Substring(1, 2); // returns "bc"
"abc".Substring(1, 6); // error
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="lisp">
;; Examples in Common Lisp
(subseq "abc" 1 2) ; returns "b"
(subseq "abc" 2) ; returns "c"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="erlang">
% Examples in Erlang
string:substr("abc", 2, 1). % returns "b"
string:substr("abc", 2). % returns "bc"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="python">
# Examples in Python
"abc"[1:2] # returns "b"
"abc"[1:3] # returns "bc"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="rexx">
/* Examples in Rexx */
substr("abc", 2, 1) /* returns "b" */
Line 2,571:
substr("abc", 2, 6) /* returns "bc " */
substr("abc", 2, 6, "*") /* returns "bc****" */
</syntaxhighlight>
</source>
 
<!-- endsection -->
Line 2,688:
|}
 
<sourcesyntaxhighlight lang="csharp">
// Example in C#
"Wiki means fast?".ToUpper(); // "WIKI MEANS FAST?"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="rexx">
/* Example in Rexx */
translate("Wiki means fast?") /* "WIKI MEANS FAST?" */
Line 2,704:
A='upper using Translate Function.'
Translate UPPER VAR A Z /* Z="UPPER USING TRANSLATE FUNCTION." */
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="scheme">
; Example in Scheme
(use-modules (srfi srfi-13))
(string-upcase "Wiki means fast?") ; "WIKI MEANS FAST?"
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="vbvbnet">
' Example in Visual Basic
UCase("Wiki means fast?") ' "WIKI MEANS FAST?"
</syntaxhighlight>
</source>
<!-- endsection -->
 
Line 2,809:
In [[AWK programming language|AWK]], one can use regular expressions to trim:
 
<sourcesyntaxhighlight lang="awk">
ltrim(v) = gsub(/^[ \t]+/, "", v)
rtrim(v) = gsub(/[ \t]+$/, "", v)
trim(v) = ltrim(v); rtrim(v)
</syntaxhighlight>
</source>
 
or:
 
<sourcesyntaxhighlight lang="awk">
function ltrim(s) { sub(/^[ \t]+/, "", s); return s }
function rtrim(s) { sub(/[ \t]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
</syntaxhighlight>
</source>
 
====C/C++====
Line 2,828:
In C, programmers often combine a ltrim and rtrim to implement trim:
 
<sourcesyntaxhighlight lang="Cc">
#include <string.h>
#include <ctype.h>
Line 2,857:
ltrim(str);
}
</syntaxhighlight>
</source>
 
The [[open-source software|open source]] C++ library [[Boost library|Boost]] has several trim variants, including a standard one:<ref>{{cite web|url=http://www.boost.org/doc/html/string_algo/usage.html#id2742817 |title=Usage – 1.54.0 |publisher=Boost.org |date=2013-05-22 |accessdate=2013-08-24}}</ref>
 
<sourcesyntaxhighlight lang="cpp">
#include <boost/algorithm/string/trim.hpp>
trimmed = boost::algorithm::trim_copy("string");
</syntaxhighlight>
</source>
 
Note that with boost's function named simply <code>trim</code> the input sequence is modified in-place, and does not return a result.
Line 2,870:
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] {{webarchive |url=https://web.archive.org/web/20090802041055/http://doc.trolltech.com/4.5/qstring.html#trimmed |date=August 2, 2009 }}</ref>
 
<sourcesyntaxhighlight lang="cpp-qt">
#include <QString>
trimmed = s.trimmed();
</syntaxhighlight>
</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>{{cite web|author=dankamongmen |url=https://github.com/dankamongmen/sprezzos-kernel-packaging/blob/master/changelog |title=sprezzos-kernel-packaging/changelog at master · dankamongmen/sprezzos-kernel-packaging · GitHub |publisher=Github.com |date= |accessdate=2016-05-29}}</ref>
Line 2,880:
A trim algorithm in [[Haskell (programming language)|Haskell]]:
 
<sourcesyntaxhighlight lang="haskell">
import Data.Char (isSpace)
trim :: String -> String
trim = f . f
where f = reverse . dropWhile isSpace
</syntaxhighlight>
</source>
 
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.
Line 2,893:
The trim algorithm in [[J (programming language)|J]] is a [[functional programming|functional]] description:
 
<sourcesyntaxhighlight lang="Jj">
trim =. #~ [: (+./\ *. +./\.) ' '&~:
</syntaxhighlight>
</source>
 
That is: filter (<code>#~</code>) for non-space characters (<code>' '&~:</code>) between leading (<code>+./\</code>) and (<code>*.</code>) trailing (<code>+./\.</code>) spaces.
Line 2,902:
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:
 
<sourcesyntaxhighlight lang="javascript">
String.prototype.trim = function() {
return this.replace(/^\s+/g, "").replace(/\s+$/g, "");
};
</syntaxhighlight>
</source>
 
====Perl====
Line 2,912:
 
Example:
<sourcesyntaxhighlight lang="perl">
$string =~ s/^\s+//; # remove leading whitespace
$string =~ s/\s+$//; # remove trailing whitespace
</sourcesyntaxhighlight>
or:
<sourcesyntaxhighlight lang="perl">
$string =~ s/^\s+|\s+$//g ; # remove both leading and trailing whitespace
</sourcesyntaxhighlight>
These examples modify the value of the original variable <code>$string</code>.
 
Line 2,931:
 
Example:
<sourcesyntaxhighlight lang="perl">
$string = $string.trim; # remove leading and trailing whitespace
$string .= trim; # same thing
</sourcesyntaxhighlight>
 
====Tcl====
Line 2,941:
Example of trimming vowels:
 
<sourcesyntaxhighlight lang="tcl">
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
</syntaxhighlight>
</source>
 
====XSLT====
Line 2,952:
 
Example:
<sourcesyntaxhighlight lang="xml">
<xsl:variable name='trimmed'>
<xsl:value-of select='normalize-space(string)'/>
</xsl:variable>
</syntaxhighlight>
</source>
XSLT 2.0 includes regular expressions, providing another mechanism to perform string trimming.