fbpx
Wikipedia

Comparison of programming languages (string functions)

String functions are used in computer programming languages to manipulate a string or query information about a string (some do both).

Most programming languages that have a string datatype will have some string functions although there may be other low-level ways within each language to handle strings directly. In object-oriented languages, string functions are often implemented as properties and methods of string objects. In functional and list-based languages a string is represented as a list (of character codes), therefore all list-manipulation procedures could be considered string functions. However such languages may implement a subset of explicit string-specific functions as well.

For function that manipulate strings, modern object-oriented languages, like C# and Java have immutable strings and return a copy (in newly allocated dynamic memory), while others, like C manipulate the original string unless the programmer copies data to a new string. See for example Concatenation below.

The most basic example of a string function is the length(string) function. This function returns the length of a string literal.

e.g. length("hello world") would return 11.

Other languages may have string functions with similar or exactly the same syntax or parameters or outcomes. For example, in many languages the length function is usually represented as len(string). The below list of common functions aims to help limit this confusion.

Common string functions (multi language reference) edit

String functions common to many languages are listed below, including the different names used. The below list of common functions aims to help programmers find the equivalent function in a language. Note, string concatenation and regular expressions are handled in separate pages. Statements in guillemets (« … ») are optional.

CharAt edit

Definition charAt(string,integer) returns character.
Description Returns character at index in the string.
Equivalent See substring of length 1 character.
Format Languages Base index
string[i] ALGOL 68, APL, Julia, Pascal, Object Pascal (Delphi), Seed7 1
string[i] C, C++, C#, Cobra, D, FreeBASIC, Go, Python,[1] PHP, Ruby,[1] Windows PowerShell, JavaScript, APL 0
string{i} PHP (deprecated in 5.3) 0
string(i) Ada ≥1
Mid(string,i,1) VB 1
MID$(string,i,1) BASIC 1
string.Chars(i) VB.NET 0
string(i:i) Fortran 1
string.charAt(i) Java, JavaScript 0
string.[i] OCaml, F# 0
string.chars().nth(i) Rust[2] 0
string[i,1] Pick Basic 1
String.sub (string, i) Standard ML 0
string !! i Haskell 0
(string-ref string i) Scheme 0
(char string i) Common Lisp 0
(elt string i) ISLISP 0
(get string i) Clojure 0
substr(string, i, 1) Perl 5[1] 0
substr(string, i, 1)
string.substr(i, 1)
Raku[3] 0
substr(string, i, 1) PL/I 1
string.at(i) C++ (STL) (w/ bounds checking) 0
lists:nth(i, string) Erlang 1
[string characterAtIndex:i] Objective-C (NSString * only) 0
string.sub(string, i, i)
(string):sub(i, i)
Lua[1] 1
string at: i Smalltalk (w/ bounds checking) 1
string index string i Tcl 0
StringTake[string, {i}] Mathematica, Wolfram Language[1] 1
string@i Eiffel 1
string (i:1) COBOL 1
${string_param:i:1} Bash 0
istring APL 0 or 1
{ Example in Pascal } var  MyStr: string = 'Hello, World';  MyChar: Char; begin  MyChar := MyStr[2]; // 'e' 
# Example in ALGOL 68 # "Hello, World"[2]; // 'e' 
// Example in C #include <stdio.h> // for printf char MyStr[] = "Hello, World"; printf("%c", *(MyStr+1)); // 'e' printf("%c", *(MyStr+7)); // 'W' printf("%c", MyStr[11]); // 'd' printf("%s", MyStr); // 'Hello, World' printf("%s", "Hello(2), World(2)"); // 'Hello(2), World(2)' 
// Example in C++ #include <iostream> // for "cout" #include <string.h> // for "string" data type using namespace std; char MyStr1[] = "Hello(1), World(1)"; string MyStr2 = "Hello(2), World(2)"; cout << "Hello(3), World(3)"; // 'Hello(3), World(3)' cout << MyStr2[6]; // '2' cout << MyStr1.substr (5, 3); // '(1)' 
// Example in C# "Hello, World"[2]; // 'l' 
# Example in Perl 5 substr("Hello, World", 1, 1); # 'e' 
# Examples in Python "Hello, World"[2] # 'l' "Hello, World"[-3] # 'r' 
# Example in Raku "Hello, World".substr(1, 1); # 'e' 
' Example in Visual Basic Mid("Hello, World",2,1) 
' Example in Visual Basic .NET "Hello, World".Chars(2) ' "l"c 
" Example in Smalltalk " 'Hello, World' at: 2. "$e" 
//Example in Rust "Hello, World".chars().nth(2); // Some('l') 

Compare (integer result) edit

Definition compare(string1,string2) returns integer.
Description Compares two strings to each other. If they are equivalent, a zero is returned. Otherwise, most of these routines will return a positive or negative result corresponding to whether string1 is lexicographically greater than, or less than, respectively, than string2. The exceptions are the Scheme and Rexx routines which return the index of the first mismatch, and Smalltalk which answer a comparison code telling how the receiver sorts relative to string parameter.
Format Languages
IF string1<string2 THEN -1 ELSE ABS (string1>string2) FI ALGOL 68
cmp(string1, string2) Python 2
(string1 > string2) - (string1 < string2) Python
strcmp(string1, string2) C, PHP
std.string.cmp(string1, string2) D
StrComp(string1, string2) VB, Object Pascal (Delphi)
string1 cmp string2 Perl, Raku
string1 compare: string2 Smalltalk (Squeak, Pharo)
string1 <=> string2 Ruby, C++ (STL, C++20)[4]
string1.compare(string2) C++ (STL), Swift (Foundation)
compare(string1, string2) Rexx, Seed7
CompareStr(string1, string2) Pascal, Object Pascal (Delphi)
string1.compareTo(string2) Cobra, Java
string1.CompareTo(string2) VB .NET, C#, F#
(compare string1 string2) Clojure
(string= string1 string2) Common Lisp
(string-compare string1 string2 p< p= p>) Scheme (SRFI 13)
(string= string1 string2) ISLISP
compare string1 string2 OCaml
String.compare (string1, string2) Standard ML[5]
compare string1 string2 Haskell[6]
[string]::Compare(string1, string2) Windows PowerShell
[string1 compare:string2] Objective-C (NSString * only)
LLT(string1,string2)
LLE(string1,string2)
LGT(string1,string2)
LGE(string1,string2)
Fortran[7]
string1.localeCompare(string2) JavaScript
bytes.Compare([]byte(string1), []byte(string2)) Go
string compare string1 string2 Tcl
compare(string1,string2,count) PL/I[8]
string1.cmp(string2) Rust[9]
# Example in Perl 5 "hello" cmp "world"; # returns -1 
# Example in Python cmp("hello", "world") # returns -1 
# Examples in Raku "hello" cmp "world"; # returns Less "world" cmp "hello"; # returns More "hello" cmp "hello"; # returns Same 
/** Example in Rexx */ compare("hello", "world") /* returns index of mismatch: 1 */ 
; Example in Scheme (use-modules (srfi srfi-13)) ; returns index of mismatch: 0 (string-compare "hello" "world" values values values) 

Compare (relational operator-based, Boolean result) edit

Definition string1 OP string2 OR (compare string1 string2) returns Boolean.
Description Lexicographically compares two strings using a relational operator or function. Boolean result returned.
Format Languages
string1 OP string2, where OP can be any of =, <>, <, >, <= and >= Pascal, Object Pascal (Delphi), OCaml, Seed7, Standard ML, BASIC, VB, VB .NET, F#
string1 OP string2, where OP can be any of =, /=, ≠, <, >, <=, ≤ and ; Also: EQ, NE, LT, LE, GE and GT ALGOL 68
(stringOP? string1 string2), where OP can be any of =, -ci=, <, -ci<, >, -ci>, <=, -ci<=, >= and -ci>= (operators starting with '-ci' are case-insensitive) Scheme
(stringOP string1 string2), where OP can be any of =, -ci=, <>, -ci<>, <, -ci<, >, -ci>, <=, -ci<=, >= and -ci>= (operators starting with '-ci' are case-insensitive) Scheme (SRFI 13)
(stringOP string1 string2), where OP can be any of =, -equal, /=, -not-equal, <, -lessp, >, -greaterp, <=, -not-greaterp, >= and -not-lessp (the verbal operators are case-insensitive) Common Lisp
(stringOP string1 string2), where OP can be any of =, /=, <, >, <=, and >= ISLISP
string1 OP string2, where OP can be any of =, \=, <, >, <= and >= Rexx
string1 OP string2, where OP can be any of =, ¬=, <, >, <=, >=, ¬< and ¬> PL/I
string1 OP string2, where OP can be any of =, /=, <, >, <= and >= Ada
string1 OP string2, where OP can be any of ==, /=, <, >, =< and >= Erlang
string1 OP string2, where OP can be any of ==, /=, <, >, <= and >= Haskell
string1 OP string2, where OP can be any of eq, ne, lt, gt, le and ge Perl, Raku
string1 OP string2, where OP can be any of ==, !=, <, >, <= and >= C++ (STL), C#, D, Go, JavaScript, Python, PHP, Ruby, Rust,[10] Swift
string1 OP string2, where OP can be any of -eq, -ceq, -ne, -cne, -lt, -clt, -gt, -cgt, -le, -cle, -ge, and -cge (operators starting with 'c' are case-sensitive) Windows PowerShell
string1 OP string2, where OP can be any of ==, ~=, <, >, <= and >= Lua
string1 OP string2, where OP can be any of =, ~=, <, >, <= and >= Smalltalk
string1 OP string2, where OP can be any of ==, /=, <, >, <= and >=; Also: .EQ., .NE., .LT., .LE., .GT. and .GE. Fortran.[11]
string1 OP string2 where OP can be any of =, <>, <, >, <=, >= as well as worded equivalents COBOL
string1 OP string2 where OP can be any of ==, <>, <, >, <= and >= Cobra
string1 OP string2 is available in the syntax, but means comparison of the pointers pointing to the strings, not of the string contents. Use the Compare (integer result) function. C, Java
string1.METHOD(string2) where METHOD is any of eq, ne, gt, lt, ge, le Rust[10]
% Example in Erlang "hello" > "world". % returns false 
# Example in Raku "art" gt "painting"; # returns False "art" lt "painting"; # returns True 
# Example in Windows PowerShell "hello" -gt "world" # returns false 
;; Example in Common Lisp (string> "art" "painting") ; returns nil (string< "art" "painting") ; returns non nil 

Concatenation edit

Definition concatenate(string1,string2) returns string.
Description Concatenates (joins) two strings to each other, returning the combined string. Note that some languages like C have mutable strings, so really the second string is being appended to the first string and the mutated string is returned.
Format Languages
string1 & string2 Ada, FreeBASIC, Seed7, BASIC, VB, VB .NET, COBOL (between literals only)
strcat(string1, string2) C, C++ (char * only)[12]
string1 . string2 Perl, PHP
string1 + string2 ALGOL 68, C++ (STL), C#, Cobra, FreeBASIC, Go, Pascal, Object Pascal (Delphi), Java, JavaScript, Windows PowerShell, Python, Ruby, Rust,[13] F#, Swift, Turing, VB
string1 ~ string2 D, Raku
(string-append string1 string2) Scheme, ISLISP
(concatenate 'string string1 string2) Common Lisp
(str string1 string2) Clojure
string1 || string2 Rexx, SQL, PL/I
string1 // string2 Fortran
string1 ++ string2 Erlang, Haskell
string1 ^ string2 OCaml, Standard ML, F#
[string1 stringByAppendingString:string2] Objective-C (NSString * only)
string1 .. string2 Lua
string1 , string2 Smalltalk, APL
string1 string2 SNOBOL
string1string2 Bash
string1 <> string2 Mathematica
concat string1 string2 Tcl
{ Example in Pascal } 'abc' + 'def'; // returns "abcdef" 
// Example in C# "abc" + "def"; // returns "abcdef" 
' Example in Visual Basic "abc" & "def" ' returns "abcdef" "abc" + "def" ' returns "abcdef" "abc" & Null ' returns "abc" "abc" + Null ' returns Null 
// Example in D "abc" ~ "def"; // returns "abcdef" 
;; Example in common lisp (concatenate 'string "abc " "def " "ghi") ; returns "abc def ghi" 
# Example in Perl 5 "abc" . "def"; # returns "abcdef" "Perl " . 5; # returns "Perl 5" 
# Example in Raku "abc" ~ "def"; # returns "abcdef" "Perl " ~ 6; # returns "Perl 6" 

Contains edit

Definition contains(string,substring) returns boolean
Description Returns whether string contains substring as a substring. This is equivalent to using Find and then detecting that it does not result in the failure condition listed in the third column of the Find section. However, some languages have a simpler way of expressing this test.
Related Find
Format Languages
string_in_string(string, loc int, substring) ALGOL 68
ContainsStr(string, substring) Object Pascal (Delphi)
strstr(string, substring) != NULL C, C++ (char * only)
string.Contains(substring) C#, VB .NET, Windows PowerShell, F#
string.contains(substring) Cobra, Java (1.5+), Raku, Rust,[14] C++ (C++23)[15]
string.indexOf(substring) >= 0 JavaScript
strpos(string, substring) !== false PHP
str_contains(string, substring) PHP (8+)
pos(string, substring) <> 0 Seed7
substring in string Cobra, Python (2.3+)
string.find(string, substring) ~= nil Lua
string.include?(substring) Ruby
Data.List.isInfixOf substring string Haskell (GHC 6.6+)
string includesSubstring: substring Smalltalk (Squeak, Pharo, Smalltalk/X)
String.isSubstring substring string Standard ML
(search substring string) Common Lisp
(not (null (string-index substring string))) ISLISP
(substring? substring string) Clojure
! StringFreeQ[string, substring] Mathematica
index(string, substring, startpos)>0 Fortran, PL/I[16]
index(string, substring, occurrence)>0 Pick Basic
strings.Contains(string, substring) Go
string.find(substring) != string::npos C++
[string containsString:substring] Objective-C (NSString * only, iOS 8+/OS X 10.10+)
string.rangeOfString(substring) != nil Swift (Foundation)
∨/substringstring APL
¢ Example in ALGOL 68 ¢ string in string("e", loc int, "Hello mate"); ¢ returns true ¢ string in string("z", loc int, "word"); ¢ returns false ¢ 
// Example In C# "Hello mate".Contains("e"); // returns true "word".Contains("z"); // returns false 
# Example in Python "e" in "Hello mate" # returns true "z" in "word" # returns false 
# Example in Raku "Good morning!".contains('z') # returns False "¡Buenos días!".contains('í'); # returns True 
" Example in Smalltalk " 'Hello mate' includesSubstring: 'e' " returns true " 'word' includesSubstring: 'z' " returns false " 

Equality edit

Tests if two strings are equal. See also #Compare and #Compare. Note that doing equality checks via a generic Compare with integer result is not only confusing for the programmer but is often a significantly more expensive operation; this is especially true when using "C-strings".

Format Languages
string1 == string2 Python, C++ (STL), C#, Cobra, Go, JavaScript (similarity), PHP (similarity), Ruby, Rust,[10] Erlang, Haskell, Lua, D, Mathematica, Swift
string1 === string2 JavaScript, PHP
string1 == string2
string1 .EQ. string2
Fortran
strcmp(string1, string2) == 0 C
(string=? string1 string2) Scheme
(string= string1 string2) Common Lisp, ISLISP
string1 = string2 ALGOL 68, Ada, Object Pascal (Delphi), OCaml, Pascal, Rexx, Seed7, Standard ML, BASIC, VB, VB .NET, F#, Smalltalk, PL/I, COBOL
test string1 = string2
[ string1 = string2 ]
Bourne Shell
string1 eq string2 Perl, Raku
string1.equals(string2) Cobra, Java
string1.Equals(string2) C#
string1 -eq string2
[string]::Equals(string1, string2)
Windows PowerShell
[string1 isEqualToString:string2]
[string1 isEqual:string2]
Objective-C (NSString * only)
string1string2 APL
string1.eq(string2) Rust[10]
// Example in C# "hello" == "world" // returns false 
' Example in Visual Basic "hello" = "world" ' returns false 
# Examples in Perl 5 'hello' eq 'world' # returns 0 'hello' eq 'hello' # returns 1 
# Examples in Raku 'hello' eq 'world' # returns False 'hello' eq 'hello' # returns True 
# Example in Windows PowerShell "hello" -eq "world" # returns false 
⍝ Example in APL 'hello'  'world' ⍝ returns 0 


Find edit

Definition find(string,substring) returns integer
Description Returns the position of the start of the first occurrence of substring in string. If the substring is not found most of these routines return an invalid index value – -1 where indexes are 0-based, 0 where they are 1-based – or some value to be interpreted as Boolean FALSE.
Related instrrev
Format Languages If not found
string in string(substring, pos, string[startpos:]) ALGOL 68 returns BOOL: TRUE or FALSE, and position in REF INT pos.
InStr(«startposstring,substring) VB (positions start at 1) returns 0
INSTR$(string,substring) BASIC (positions start at 1) returns 0
index(string,substring) AWK returns 0
index(string,substring«,startpos») Perl 5 returns −1
index(string,substring«,startpos»)
string.index(substring,«,startpos»)
Raku returns Nil
instr(«startposstring,substring) FreeBASIC returns 0
strpos(string,substring«,startpos») PHP returns FALSE
locate(string, substring) Ingres returns string length + 1
strstr(string, substring) C, C++ (char * only, returns pointer to first character) returns NULL
std.string.indexOf(string, substring) D returns −1
pos(string, substring«, startpos») Seed7 returns 0
strings.Index(string, substring) Go returns −1
pos(substring, string) Pascal, Object Pascal (Delphi) returns 0
pos(substring, string«,startpos») Rexx returns 0
string.find(substring«,startpos») C++ (STL) returns std::string::npos
string.find(substring«,startpos«,endpos»») Python returns −1
string.index(substring«,startpos«,endpos»») raises ValueError
string.index(substring«,startpos») Ruby returns nil
string.indexOf(substring«,startpos») Java, JavaScript returns −1
string.IndexOf(substring«,startpos«, charcount»») VB .NET, C#, Windows PowerShell, F# returns −1
string:str(string, substring) Erlang returns 0
(string-contains string substring) Scheme (SRFI 13) returns #f
(search substring string) Common Lisp returns NIL
(string-index substring string) ISLISP returns nil
List.findIndex (List.isPrefixOf substring) (List.tails string) Haskell (returns only index) returns Nothing
Str.search_forward (Str.regexp_string substring) string 0 OCaml raises Not_found
Substring.size (#1 (Substring.position substring (Substring.full string))) Standard ML returns string length
[string rangeOfString:substring].location Objective-C (NSString * only) returns NSNotFound
string.find(string, substring)
(string):find(substring)
Lua returns nil
string indexOfSubCollection: substring startingAt: startpos ifAbsent: aBlock
string findString: substring startingAt: startpos
Smalltalk (Squeak, Pharo) evaluate aBlock which is a block closure (or any object understanding value)
returns 0
startpos = INDEX(string, substring «,back» «, kind») Fortran returns 0 if substring is not in string; returns LEN(string)+1 if substring is empty
POSITION(substring IN string) SQL returns 0 (positions start at 1)
index(string, substring, startpos ) PL/I[16] returns 0 (positions start at 1)
index(string, substring, occurrence ) Pick Basic returns 0 if occurrence of substring is not in string; (positions start at 1)
string.indexOf(substring«,startpos«, charcount»») Cobra returns −1
string first substring string startpos Tcl returns −1
(substringstring)⍳1 APL returns 1 + the last position in string
string.find(substring) Rust[17] returns None

Examples

  • Common Lisp
    (search "e" "Hello mate") ; returns 1 (search "z" "word") ; returns NIL 
  • C#
    "Hello mate".IndexOf("e"); // returns 1 "Hello mate".IndexOf("e", 4); // returns 9 "word".IndexOf("z"); // returns -1 
  • Raku
    "Hello, there!".index('e') # returns 1 "Hello, there!".index('z') # returns Nil 
  • Scheme
    (use-modules (srfi srfi-13)) (string-contains "Hello mate" "e") ; returns 1 (string-contains "word" "z") ; returns #f 
  • Visual Basic
    ' Examples in InStr("Hello mate", "e") ' returns 2 InStr(5, "Hello mate", "e") ' returns 10 InStr("word", "z") ' returns 0 
  • Smalltalk
    'Hello mate' indexOfSubCollection:'ate' "returns 8" 
    'Hello mate' indexOfSubCollection:'late' "returns 0" 
    I'Hello mate' indexOfSubCollection:'late' ifAbsent:[ 99 ] "returns 99" 
    'Hello mate' indexOfSubCollection:'late' ifAbsent:[ self error ] "raises an exception" 


Find character edit

Definition find_character(string,char) returns integer
Description Returns the position of the start of the first occurrence of the character char in string. If the character is not found most of these routines return an invalid index value – -1 where indexes are 0-based, 0 where they are 1-based – or some value to be interpreted as Boolean FALSE. This can be accomplished as a special case of #Find, with a string of one character; but it may be simpler or more efficient in many languages to locate just one character. Also, in many languages, characters and strings are different types, so it is convenient to have such a function.
Related find
Format Languages If not found
char in string(char, pos, string[startpos:]) ALGOL 68 returns BOOL: TRUE or FALSE, and position in REF INT pos.
instr(string, any char«,startpos») (char, can contain more them one char, in which case the position of the first appearance of any of them is returned.) FreeBASIC returns 0
strchr(string,char) C, C++ (char * only, returns pointer to character) returns NULL
std.string.find(string, dchar) D returns −1
string.find(char«,startpos») C++ (STL) returns std::string::npos
pos(string, char«, startpos») Seed7 returns 0
strings.IndexRune(string,char) Go returns −1
string.indexOf(char«,startpos») Java, JavaScript returns −1
string.IndexOf(char«,startpos«, charcount»») VB .NET, C#, Windows PowerShell, F# returns −1
(position char string) Common Lisp returns NIL
(char-index char string) ISLISP returns nil
List.elemIndex char string Haskell (returns Just index) returns Nothing
String.index string char OCaml raises Not_found
position = SCAN (string, set «, back» «, kind»)
position = VERIFY (string, set «, back» «, kind»)[a]
Fortran returns zero
string indexOf: char ifAbsent: aBlock
string indexOf: char
string includes: char
Smalltalk evaluate aBlock which is a BlockClosure (or any object understanding value)
returns 0
returns true or false
index(string, char, startpos ) PL/I[18] returns 0 (positions start at 1)
string.index(?char) Ruby returns nil
strpos(string,char,startpos) PHP returns false
string.indexOf(char«,startpos«, charcount»») Cobra returns −1
stringchar APL returns 1 + the last position in string
string.find(substring) Rust[17] returns None
// Examples in C# "Hello mate".IndexOf('e'); // returns 1 "word".IndexOf('z') // returns -1 
; Examples in Common Lisp (position #\e "Hello mate") ; returns 1 (position #\z "word") ; returns NIL 

^a Given a set of characters, SCAN returns the position of the first character found,[19] while VERIFY returns the position of the first character that does not belong to the set.[20]

Format edit

Definition format(formatstring, items) returns string
Description Returns the formatted string representation of one or more items.
Format Languages Format string syntax
associate(file, string); putf(file, $formatstring$, items) ALGOL 68 ALGOL
Format(item, formatstring) VB
sprintf(formatstring, items) Perl, PHP, Raku, Ruby C
item.fmt(formatstring) Raku C
io_lib:format(formatstring, items) Erlang
sprintf(outputstring, formatstring, items) C C
std::format(formatstring, items) C++ (C++20) Python
std.string.format(formatstring, items) D C
Format(formatstring, items) Object Pascal (Delphi)
fmt.Sprintf(formatstring, items) Go C
printf formatstring items Unix C
formatstring % (items) Python, Ruby C
formatstring.format(items) Python .NET
fformatstring Python 3
Printf.sprintf formatstring[21] items OCaml, F# C
Text.Printf.printf formatstring items Haskell (GHC) C
formatstring printf: items Smalltalk C
String.format(formatstring, items) Java C
String.Format(formatstring, items) VB .NET, C#, F# .NET
(format formatstring items) Scheme (SRFI 28) Lisp
(format nil formatstring items) Common Lisp Lisp
(format formatstring items) Clojure Lisp
formatstring -f items Windows PowerShell .NET
[NSString stringWithFormat:formatstring, items] Objective-C (NSString * only) C
String(format:formatstring, items) Swift (Foundation) C
string.format(formatstring, items)
(formatstring):format(items)
Lua C
WRITE (outputstring, formatstring) items Fortran Fortran
put string(string) edit(items)(format) PL/I PL/I (similar to Fortran)
String.format(formatstring, items) Cobra .NET
format formatstring items Tcl C
formatnumbersitems
formatstring ⎕FMT items
APL APL
format!(formatstring, items) Rust[22] Python
// Example in C# String.Format("My {0} costs {1:C2}", "pen", 19.99); // returns "My pen costs $19.99" 
// Example in Object Pascal (Delphi) Format('My %s costs $%2f', ['pen', 19.99]); // returns "My pen costs $19.99" 
// Example in Java String.format("My %s costs $%2f", "pen", 19.99); // returns "My pen costs $19.99" 
# Examples in Raku sprintf "My %s costs \$%.2f", "pen", 19.99; # returns "My pen costs $19.99" 1.fmt("%04d"); # returns "0001" 
# 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" 
#Example in Python 3.6+ pen = "pen" f"My {pen} costs {19.99}" #returns "My pen costs 19.99" 
; Example in Scheme (format "My ~a costs $~1,2F" "pen" 19.99) ; returns "My pen costs $19.99" 
/* 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" */ 

Inequality edit

Tests if two strings are not equal. See also #Equality.

Format Languages
string1 ne string2
string1 NE string2
ALGOL 68 – note: the operator "ne" is literally in bold type-font.
string1 /= string2 ALGOL 68, Ada, Erlang, Fortran, Haskell
string1 <> string2 BASIC, VB, VB .NET, Pascal, Object Pascal (Delphi), OCaml, PHP, Seed7, Standard ML, F#, COBOL, Cobra, Python 2 (deprecated)
string1 # string2 BASIC (some implementations)
string1 ne string2 Perl, Raku
(string<> string1 string2) Scheme (SRFI 13)
(string/= string1 string2) Common Lisp
(string/= string1 string2) ISLISP
(not= string1 string2) Clojure
string1 != string2 C++ (STL), C#, Go, JavaScript (not similar), PHP (not similar), Python, Ruby, Rust,[10] Swift, D, Mathematica
string1 !== string2 JavaScript, PHP
string1 \= string2 Rexx
string1 ¬= string2 PL/I
test string1 != string2
[ string1 != string2 ]
Bourne Shell
string1 -ne string2
-not [string]::Equals(string1, string2)
Windows PowerShell
string1 ~= string2 Lua, Smalltalk
string1string2 APL
string1.ne(string2) Rust[10]
// Example in C# "hello" != "world" // returns true 
' Example in Visual Basic "hello" <> "world" ' returns true 
;; Example in Clojure (not= "hello" "world") ; ⇒ true 
# Example in Perl 5 'hello' ne 'world' # returns 1 
# Example in Raku 'hello' ne 'world' # returns True 
# Example in Windows PowerShell "hello" -ne "world" # returns true 

index edit

see #Find

indexof edit

see #Find

instr edit

see #Find

instrrev edit

see #rfind

join edit

Definition join(separator, list_of_strings) returns a list of strings joined with a separator
Description Joins the list of strings into a new string, with the separator string between each of the substrings. Opposite of split.
Related sprintf
Format Languages
std.string.join(array_of_strings, separator) D
string:join(list_of_strings, separator) Erlang
join(separator, list_of_strings) Perl, PHP, Raku
implode(separator, array_of_strings) PHP
separator.join(sequence_of_strings) Python, Swift 1.x
array_of_strings.join(separator) Ruby, JavaScript, Raku, Rust[23]
(string-join array_of_strings separator) Scheme (SRFI 13)
(format nil "~{~a~^separator~}" array_of_strings) Common Lisp
(clojure.string/join separator list_of_strings)
(apply str (interpose separator list_of_strings))
Clojure
strings.Join(array_of_strings, separator) Go
join(array_of_strings, separator) Seed7
String.concat separator list_of_strings OCaml
String.concatWith separator list_of_strings Standard ML
Data.List.intercalate separator list_of_strings Haskell (GHC 6.8+)
Join(array_of_strings, separator) VB
String.Join(separator, array_of_strings) VB .NET, C#, F#
String.join(separator, array_of_strings) Java 8+
&{$OFS=$separator; "$array_of_strings"}
array_of_strings -join separator
Windows PowerShell
[array_of_strings componentsJoinedByString:separator] Objective-C (NSString * only)
table.concat(table_of_strings, separator) Lua
{|String streamContents: [ :stream | collectionOfAnything asStringOn: stream delimiter: separator ]
collectionOfAnything joinUsing: separator
Smalltalk (Squeak, Pharo)
array_of_strings.join(separator«, final_separator») Cobra
sequence_of_strings.joinWithSeparator(separator) Swift 2.x
1↓∊separatorlist_of_strings APL
// Example in C# String.Join("-", {"a", "b", "c"}) // "a-b-c" 
" Example in Smalltalk " #('a' 'b' 'c') joinUsing: '-' " 'a-b-c' " 
# Example in Perl 5 join( '-', ('a', 'b', 'c')); # 'a-b-c' 
# Example in Raku <a b c>.join('-'); # 'a-b-c' 
# Example in Python "-".join(["a", "b", "c"]) # 'a-b-c' 
# Example in Ruby ["a", "b", "c"].join("-") # 'a-b-c' 
; Example in Scheme (use-modules (srfi srfi-13)) (string-join '("a" "b" "c") "-") ; "a-b-c" 

lastindexof edit

see #rfind

left edit

Definition left(string,n) returns string
Description Returns the left n part of a string. If n is greater than the length of the string then most implementations return the whole string (exceptions exist – see code examples). Note that for variable-length encodings such as UTF-8, UTF-16 or Shift-JIS, it can be necessary to remove string positions at the end, in order to avoid invalid strings.
Format Languages
string (string'First .. string'First + n - 1) Ada
substr(string, 0, n) AWK (changes string), Perl, PHP, Raku
LEFT$(string,n) BASIC, VB
left(string,n) VB, FreeBASIC, Ingres, Pick Basic
strncpy(string2, string, n) C standard library
string.substr(0,n) C++ (STL), Raku
[string substringToIndex:n] Objective-C (NSString * only)
(apply str (take n string)) Clojure
string[0 .. n] D[24]
string:substr(string, start, length) Erlang
(subseq string 0 n) Common Lisp
string[:n] Cobra, Go, Python
left(string,n «,padchar») Rexx, Erlang
string[0, n]
string[0..n - 1]
Ruby
string[1, n] Pick Basic
string[ .. n] Seed7
string.Substring(0,n) VB .NET, C#, Windows PowerShell, F#
leftstr(string, n) Pascal, Object Pascal (Delphi)
copy (string,1,n) Turbo Pascal
string.substring(0,n) Java,[25] JavaScript
(string-take string n) Scheme (SRFI 13)
take n string Haskell
String.extract (string, n, NONE) Standard ML
String.sub string 0 n OCaml[26]
string.[..n] F#
string.sub(string, 1, n)
(string):sub(1, n)
Lua
string first: n Smalltalk (Squeak, Pharo)
string(:n) Fortran
StringTake[string, n] Mathematica[27]
string («FUNCTION» LENGTH(string) - n:n) COBOL
string.substring(0, n) Cobra
nstring. APL
string[0..n]
string[..n]
string.get(0..n)
string.get(..n)
Rust[28]
# Example in Raku "Hello, there!".substr(0, 6); # returns "Hello," 
/* Examples in Rexx */ left("abcde", 3) /* returns "abc" */ left("abcde", 8) /* returns "abcde " */ left("abcde", 8, "*") /* returns "abcde***" */ 
; Examples in Scheme (use-modules (srfi srfi-13)) (string-take "abcde", 3) ; returns "abc" (string-take "abcde", 8) ; error 
' Examples in Visual Basic Left("sandroguidi", 3) ' returns "san" Left("sandroguidi", 100) ' returns "sandroguidi" 


len edit

see #length


length edit

Definition length(string) returns an integer number
Description Returns the length of a string (not counting the null terminator or any other of the string's internal structural information). An empty string returns a length of 0.
Format Returns Languages
string'Length Ada
UPB string ALGOL 68
echo "${#string_param}" Bash
length(string) Ingres, Perl 5, Pascal, Object Pascal (Delphi), Rexx, Seed7, SQL, PL/I
len(string) BASIC, FreeBASIC, Python, Go, Pick Basic
length(string), string:len(string) Erlang
Len(string) VB, Pick Basic
string.Length Number of UTF-16 code units VB .NET, C#, Windows PowerShell, F#
chars(string)
string.chars
Number of graphemes (NFG) Raku
codes(string)
string.codes
Number of Unicode code points Raku
string.size OR string.length Number of bytes[29] Ruby
strlen(string) Number of bytes C, PHP
string.length() C++ (STL)
string.length Cobra, D, JavaScript
string.length() Number of UTF-16 code units Java
(string-length string) Scheme
(length string) Common Lisp, ISLISP
(count string) Clojure
String.length string OCaml
size string Standard ML
length string Number of Unicode code points Haskell
string.length Number of UTF-16 code units Objective-C (NSString * only)
string.characters.count Number of characters Swift (2.x)
count(string) Number of characters Swift (1.2)
countElements(string) Number of characters Swift (1.0–1.1)
string.len(string)
(string):len()
#string
Lua
string size Smalltalk
LEN(string)
LEN_TRIM(string)
Fortran
StringLength[string] Mathematica
«FUNCTION» LENGTH(string) or

«FUNCTION» BYTE-LENGTH(string)

number of characters and number of bytes, respectively COBOL
string length string a decimal string giving the number of characters Tcl
string APL
string.len() Number of bytes Rust[30]
string.chars().count() Number of Unicode code points Rust[31]
// Examples in C# "hello".Length; // returns 5 "".Length; // returns 0 
# Examples in Erlang string:len("hello"). % returns 5 string:len(""). % returns 0 
# Examples in Perl 5 length("hello"); # returns 5 length(""); # returns 0 
# Examples in Raku "🏳️‍🌈".chars; chars "🏳️‍🌈"; # both return 1 "🏳️‍🌈".codes; codes "🏳️‍🌈"; # both return 4 "".chars; chars ""; # both return 0 "".codes; codes ""; # both return 0 
' Examples in Visual Basic Len("hello") ' returns 5 Len("") ' returns 0 
//Examples in Objective-C [@"hello" Length] //returns 5 [@"" Length] //returns 0 
-- Examples in Lua ("hello"):len() -- returns 5 #"" -- returns 0 

locate edit

see #Find


Lowercase edit

Definition lowercase(string) returns string
Description Returns the string in lower case.
Format Languages
LCase(string) VB
lcase(string) FreeBASIC
lc(string) Perl, Raku
string.lc Raku
tolower(char) C[32]
std.string.toLower(string) D
transform(string.begin(), string.end(), result.begin(), ::tolower)[33] C++[34]
lowercase(string) Object Pascal (Delphi)
strtolower(string) PHP
lower(string) Seed7
${string_param,,} Bash
echo "string" | tr 'A-Z' 'a-z' Unix
string.lower() Python
downcase(string) Pick Basic
string.downcase Ruby[35]
strings.ToLower(string) Go
(string-downcase string) Scheme (R6RS), Common Lisp
(lower-case string) Clojure
String.lowercase string OCaml
String.map Char.toLower string Standard ML
map Char.toLower string Haskell
string.toLowerCase() Java, JavaScript
to_lower(string) Erlang
string.ToLower() VB .NET, C#, Windows PowerShell, F#
string.lowercaseString Objective-C (NSString * only), Swift (Foundation)
string.lower(string)
(string):lower()
Lua
string asLowercase Smalltalk
LOWER(string) SQL
lowercase(string) PL/I[8]
ToLowerCase[string] Mathematica
«FUNCTION» LOWER-CASE(string) COBOL
string.toLower Cobra
string tolower string Tcl
string.to_lowercase() Rust[36]
// Example in C# "Wiki means fast?".ToLower(); // "wiki means fast?" 
; Example in Scheme (use-modules (srfi srfi-13)) (string-downcase "Wiki means fast?") ; "wiki means fast?" 
/* Example in C */ #include <ctype.h> #include <stdio.h> int main(void) {  char string[] = "Wiki means fast?";  int i;  for (i = 0; i < sizeof(string) - 1; ++i) {  /* transform characters in place, one by one */  string[i] = tolower(string[i]);  }  puts(string); /* "wiki means fast?" */  return 0; } 
# Example in Raku "Wiki means fast?".lc; # "wiki means fast?" 


mid edit

see #substring


partition edit

Definition <string>.partition(separator) returns the sub-string before the separator; the separator; then the sub-string after the separator.
Description Splits the given string by the separator and returns the three substrings that together make the original.
Format Languages Comments
string.partition(separator) Python, Ruby(1.9+)
lists:partition(pred, string) Erlang
split /(separator)/, string, 2 Perl 5
split separator, string, 2
string.split( separator, 2 )
Raku Separator does not have to be a regular expression
# 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', "", "") 
# Examples in Perl 5 / Raku 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'); 


replace edit

Definition replace(string, find, replace) returns string
Description Returns a string with find occurrences changed to replace.
Format Languages
changestr(find, string, replace) Rexx
std.string.replace(string, find, replace) D
Replace(string, find, replace) VB
replace(string, find, replace) Seed7
change(string, find, replace) Pick Basic
string.Replace(find, replace) C#, F#, VB .NET
str_replace(find, replace, string) PHP
re:replace(string, find, replace, «{return, list}») Erlang
string.replace(find, replace) Cobra, Java (1.5+), Python, Rust[37]
string.replaceAll(find_regex, replace)[38] Java
string.gsub(find, replace) Ruby
string =~ s/find_regex/replace/g[38] Perl 5
string.subst(find, replace, :g) Raku
string.replace(find, replace, "g") [39]
string.replace(/find_regex/g, replace)[38]
JavaScript
echo "string" | sed 's/find_regex/replace/g'[38] Unix
${string_param//find_pattern/replace} Bash
string.replace(find, replace)
string -replace find_regex, replace[38]
Windows PowerShell
Str.global_replace (Str.regexp_string find) replace string OCaml
[string stringByReplacingOccurrencesOfString:find withString:replace] Objective-C (NSString * only)
string.stringByReplacingOccurrencesOfString(find, withString:replace) Swift (Foundation)
string.gsub(string, find, replace)
(string):gsub(find, replace)
Lua
string copyReplaceAll: find with: replace Smalltalk (Squeak, Pharo)
string map {find replace} string Tcl
StringReplace[string, find -> replace] Mathematica
strings.Replace(string, find, replace, -1) Go
INSPECT string REPLACING ALL/LEADING/FIRST find BY replace COBOL
find_regex ⎕R replace_regexstring APL
// Examples in C# "effffff".Replace("f", "jump"); // returns "ejumpjumpjumpjumpjumpjump" "blah".Replace("z", "y"); // returns "blah" 
// Examples in Java "effffff".replace("f", "jump"); // returns "ejumpjumpjumpjumpjumpjump" "effffff".replaceAll("f*", "jump"); // returns "ejump" 
// Examples in Raku "effffff".subst("f", "jump", :g); # returns "ejumpjumpjumpjumpjumpjump" "blah".subst("z", "y", :g); # returns "blah" 
' Examples in Visual Basic Replace("effffff", "f", "jump") ' returns "ejumpjumpjumpjumpjumpjump" Replace("blah", "z", "y") ' returns "blah" 
# Examples in Windows PowerShell "effffff" -replace "f", "jump" # returns "ejumpjumpjumpjumpjumpjump" "effffff" -replace "f*", "jump" # returns "ejump" 

reverse edit

Definition reverse(string)
Description Reverses the order of the characters in the string.
Format Languages
reverse string Perl 5, Haskell
flip string
string.flip
Raku
lists:reverse(string) Erlang
strrev(string) PHP
string[::-1] Python
(string-reverse string) Scheme (SRFI 13)
(reverse string) Common Lisp
string.reverse Ruby, D (modifies string)
new StringBuilder(string).reverse().toString() Java
std::reverse(string.begin(), string.end()); C++ (std::string only, modifies string)
StrReverse(string) VB
string.Reverse() VB .NET, C#
implode (rev (explode string)) Standard ML
string.split("").reverse().join("") JavaScript
string.reverse(string)
(string):reverse()
Lua
string reverse Smalltalk
StringReverse[string] Mathematica
reverse(string) PL/I
«FUNCTION» REVERSE(string) COBOL
string.toCharArray.toList.reversed.join() Cobra
String(string.characters.reverse()) Swift (2.x)
String(reverse(string)) Swift (1.2)
string reverse string Tcl
string APL
string.chars().rev().collect::<String>() Rust[40]
echo string | rev Unix
" Example in Smalltalk " 'hello' reversed " returns 'olleh' " 
# Example in Perl 5 reverse "hello" # returns "olleh" 
# Example in Raku "hello".flip # returns "olleh" 
# Example in Python "hello"[::-1] # returns "olleh" 
; Example in Scheme (use-modules (srfi srfi-13)) (string-reverse "hello") ; returns "olleh" 

rfind edit

Definition rfind(string,substring) returns integer
Description Returns the position of the start of the last occurrence of substring in string. If the substring is not found most of these routines return an invalid index value – -1 where indexes are 0-based, 0 where they are 1-based – or some value to be interpreted as Boolean FALSE.
Related instr
Format Languages If not found
InStrRev(«startposstring,substring) VB returns 0
instrrev(«startposstring,substring) FreeBASIC returns 0
rindex(string,substring«,startpos») Perl 5 returns −1
rindex(string,substring«,startpos»)
string.rindex(substring«,startpos»)
Raku returns Nil
strrpos(string,substring«,startpos») PHP returns FALSE
string.rfind(substring«,startpos») C++ (STL) returns std::string::npos
std.string.rfind(string, substring) D returns −1
string.rfind(substring«,startpos«, endpos»») Python returns −1
string.rindex(substring«,startpos«, endpos»») raises ValueError
rpos(string, substring«,startpos») Seed7 returns 0
string.rindex(substring«,startpos») Ruby returns nil
strings.LastIndex(string, substring) Go returns −1
string.lastIndexOf(substring«,startpos») Java, JavaScript returns −1
string.LastIndexOf(substring«,startpos«, charcount»») VB .NET, C#, Windows PowerShell, F# returns −1
(search substring string :from-end t) Common Lisp returns NIL
[string rangeOfString:substring options:NSBackwardsSearch].location Objective-C (NSString * only) returns NSNotFound
Str.search_backward (Str.regexp_string substring) string (Str.length string - 1) OCaml raises Not_found
string.match(string, '.*()'..substring)
string:match('.*()'..substring)
Lua returns nil
Ada.Strings.Unbounded.Index(Source => string, Pattern => substring, Going => Ada.Strings.Backward) Ada returns 0
string.lastIndexOf(substring«,startpos«, charcount»») Cobra returns −1
string lastIndexOfString:substring Smalltalk returns 0
string last substring string startpos Tcl returns −1
(⌽<\⌽substring⍷'string')⍳1 APL returns −1
string.rfind(substring) Rust[41] returns None
; Examples in Common Lisp (search "e" "Hello mate" :from-end t) ; returns 9 (search "z" "word" :from-end t) ; returns NIL 
// Examples in C# "Hello mate".LastIndexOf("e"); // returns 9 "Hello mate".LastIndexOf("e", 4); // returns 1 "word".LastIndexOf("z"); // returns -1 
# Examples in Perl 5 rindex("Hello mate", "e"); # returns 9 rindex("Hello mate", "e", 4); # returns 1 rindex("word", "z"); # returns -1 
# Examples in Raku "Hello mate".rindex("e"); # returns 9 "Hello mate".rindex("e", 4); # returns 1 "word".rindex('z'); # returns Nil 
' Examples in Visual Basic InStrRev("Hello mate", "e") ' returns 10 InStrRev(5, "Hello mate", "e") ' returns 2 InStrRev("word", "z") ' returns 0 


right edit

Definition right(string,n) returns string
Description Returns the right n part of a string. If n is greater than the length of the string then most implementations return the whole string (exceptions exist – see code examples).
Format Languages
string (string'Last - n + 1 .. string'Last) Ada
Right(string,n) VB
RIGHT$(string,n) BASIC
right(string,n) FreeBASIC, Ingres, Pick Basic
strcpy(string2, string+n) (n must not be greater than the length of string) C
string.Substring(string.Length()-n) C#
string[len(string)-n:] Go
string.substring(string.length()-n) Java
string.slice(-n) JavaScript[42]
right(string,n «,padchar») Rexx, Erlang
substr(string,-n) Perl 5, PHP
substr(string,*-n)
string.substr(*-n)
Raku
string[-n:] Cobra, Python
${string_param: -n} (note the space after the colon) Bash
string[n] Pick Basic
(string-take-right string n) Scheme (SRFI 13)
string[-n..-1] Ruby
string[$-n .. $] D[43]
String.sub string (String.length string - n) n OCaml[26]
string.sub(string, -n)
(string):sub(-n)
Lua
string last: n Smalltalk (Squeak, Pharo)
StringTake[string, -n] Mathematica[27]
string (1:n) COBOL
¯nstring. APL
string[n..]
string.get(n..)
Rust[28]
// Examples in Java; extract rightmost 4 characters String str = "CarDoor"; str.substring(str.length()-4); // returns 'Door' 
# Examples in Raku "abcde".substr(*-3); # returns "cde" "abcde".substr(*-8); # 'out of range' error 
/* Examples in Rexx */ right("abcde", 3) /* returns "cde" */ right("abcde", 8) /* returns " abcde" */ right("abcde", 8, "*") /* returns "***abcde" */ 
; Examples in Scheme (use-modules (srfi srfi-13)) (string-take-right "abcde", 3) ; returns "cde" (string-take-right "abcde", 8) ; error 
' Examples in Visual Basic Right("sandroguidi", 3) ' returns "idi" Right("sandroguidi", 100) ' returns "sandroguidi" 


rpartition edit

Definition <string>.rpartition(separator) Searches for the separator from right-to-left within the string then returns the sub-string before the separator; the separator; then the sub-string after the separator.
Description Splits the given string by the right-most separator and returns the three substrings that together make the original.
Format Languages
string.rpartition(separator) Python, Ruby
# 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') 

slice edit

see #substring


split edit

Definition <string>.split(separator[, limit]) splits a string on separator, optionally only up to a limited number of substrings
Description Splits the given string by occurrences of the separator (itself a string) and returns a list (or array) of the substrings. If limit is given, after limit – 1 separators have been read, the rest of the string is made into the last substring, regardless of whether it has any separators in it. The Scheme and Erlang implementations are similar but differ in several ways. JavaScript differs also in that it cuts, it does not put the rest of the string into the last element. See the example here. The Cobra implementation will default to whitespace. Opposite of join.
Format Languages
split(/separator/, string«, limit») Perl 5
split(separator, string«, limit»)
string.split(separator, «limit»)
Raku
explode(separator, string«, limit») PHP
string.split(separator«, limit-1») Python
string.split(separator«, limit») JavaScript, Java, Ruby
string:tokens(string, sepchars) Erlang
strings.Split(string, separator)
strings.SplitN(string, separator, limit)
Go
(string-tokenize string« charset« start« end»»») Scheme (SRFI 13)
Split(string, sepchars«, limit») VB
string.Split(sepchars«, limit«, options»») VB .NET, C#, F#
string -split separator«, limit«, options»» Windows PowerShell
Str.split (Str.regexp_string separator) string OCaml
std.string.split(string, separator) D
[string componentsSeparatedByString:separator] Objective-C (NSString * only)
string.componentsSeparatedByString(separator) Swift (Foundation)
TStringList.Delimiter, TStringList.DelimitedText Object Pascal
StringSplit[string, separator«, limit»] Mathematica
string.split«(sepchars«, limit«, options»»)» Cobra
split string separator Tcl
(separatorstring)⊂string in APL2
separator(≠⊆⊢)string in Dyalog APL 16.0
APL
string.split(separator)

string.split(limit, separator)

Rust[44]
// Example in C# "abc,defgh,ijk".Split(','); // {"abc", "defgh", "ijk"} "abc,defgh;ijk".Split(',', ';'); // {"abc", "defgh", "ijk"} 
% Example in Erlang string:tokens("abc;defgh;ijk", ";"). % ["abc", "defgh", "ijk"] 
// Examples in Java "abc,defgh,ijk".split(","); // {"abc", "defgh", "ijk"} "abc,defgh;ijk".split(",|;"); // {"abc", "defgh", "ijk"} 
{ Example in Pascal } var  lStrings: TStringList;  lStr: string; begin  lStrings := TStringList.Create;  lStrings.Delimiter := ',';  lStrings.DelimitedText := 'abc,defgh,ijk';  lStr := lStrings.Strings[0]; // 'abc'  lStr := lStrings.Strings[1]; // 'defgh'  lStr := lStrings.Strings[2]; // 'ijk' end; 
# Examples in Perl 5 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') 
# Examples in Raku 'Spam eggs spam spam and ham'.split(/spam/); # (Spam eggs and ham) split(/X/, 'Spam eggs spam spam and ham'); # (Spam eggs spam spam and ham) 


sprintf edit

see #Format

strip edit

see #trim


strcmp edit

see #Compare (integer result)


substring edit

Definition substring(string, startpos, endpos) returns string
substr(string, startpos, numChars) returns string
Description Returns a substring of string between starting at startpos and endpos, or starting at startpos of length numChars. The resulting string is truncated if there are fewer than numChars characters beyond the starting point. endpos represents the index after the last character in the substring. Note that for variable-length encodings such as UTF-8, UTF-16 or Shift-JIS, it can be necessary to remove string positions at the end, in order to avoid invalid strings.
Format Languages
string[startpos:endpos] ALGOL 68 (changes base index)
string (startpos .. endpos) Ada (changes base index)
Mid(string, startpos, numChars) VB
mid(string, startpos, numChars) FreeBASIC
string[startpos+(⍳numChars)-~⎕IO] APL
MID$(string, startpos, numChars) BASIC
substr(string, startpos, numChars) AWK (changes string), Perl 5,[45][46] PHP[45][46]
substr(string, startpos, numChars)
string.substr(startpos, numChars)
Raku[47][48]
substr(string, startpos «,numChars, padChar») Rexx
string[startpos:endpos] Cobra, Python,[45][49] Go
string[startpos, numChars] Pick Basic
string[startpos, numChars]
string[startpos .. endpos-1]
string[startpos ... endpos]
Ruby[45][49]
string[startpos .. endpos]
string[startpos len numChars]
Seed7
string.slice(startpos«, endpos») JavaScript[45][49]
string.substr(startpos«, numChars») C++ (STL), JavaScript
string.Substring(startpos, numChars) VB .NET, C#, Windows PowerShell, F#
string.substring(startpos«, endpos») Java, JavaScript
copy(string, startpos, numChars) Object Pascal (Delphi)
(substring string startpos endpos) Scheme
(subseq string startpos endpos) Common Lisp
(subseq string startpos endpos) ISLISP
String.sub string startpos numChars OCaml
substring (string, startpos, numChars) Standard ML
string:sub_string(string, startpos, endpos)
string:substr(string, startpos, numChars)
Erlang
strncpy(result, string + startpos, numChars); C
string[startpos .. endpos+1] D
take numChars $ drop startpos string Haskell
[string substringWithRange:NSMakeRange(startpos, numChars)] Objective-C (NSString * only)
string.[startpos..endpos] F#
string.sub(string, startpos, endpos)
(string):sub(startpos, endpos)
Lua[45][49]
string copyFrom: startpos to: endpos Smalltalk
string(startpos:endpos) Fortran
SUBSTRING(string FROM startpos «FOR numChars») SQL
StringTake[string, {startpos, endpos}] Mathematica[45][49]
string (startpos:numChars) COBOL
${string_param:startpos:numChars} Bash
string range string startpos endpos Tcl
string[startpos..endpos]
string.get(startpos..endpos)
Rust[28]
// Examples in C# "abc".Substring(1, 1): // returns "b" "abc".Substring(1, 2); // returns "bc" "abc".Substring(1, 6); // error 
;; Examples in Common Lisp (subseq "abc" 1 2) ; returns "b" (subseq "abc" 2) ; returns "c" 
% Examples in Erlang string:substr("abc", 2, 1). % returns "b" string:substr("abc", 2). % returns "bc" 
# Examples in Perl 5 substr("abc", 1, 1); # returns "b" substr("abc", 1); # returns "bc" 
# Examples in Raku "abc".substr(1, 1); # returns "b" "abc".substr(1); # returns "bc" 
# Examples in Python "abc"[1:2] # returns "b" "abc"[1:3] # returns "bc" 
/* Examples in Rexx */ substr("abc", 2, 1) /* returns "b" */ substr("abc", 2) /* returns "bc" */ substr("abc", 2, 6) /* returns "bc " */ substr("abc", 2, 6, "*") /* returns "bc****" */ 


Uppercase edit

Definition uppercase(string) returns string
Description Returns the string in upper case.
Format Languages
UCase(string) VB
ucase(string) FreeBASIC
toupper(string) AWK (changes string)
uc(string) Perl, Raku
string.uc Raku
toupper(char) C (operates on one character)
for(size_t i = 0, len = strlen(string); i< len; i++) string[i] = toupper(string[i]);
for (char *c = string; *c != '\0'; c++) *c = toupper(*c);
C (string / char array)
std.string.toUpper(string) D
transform(string.begin(), string.end(), result.begin(), toupper)[33] C++[50]
uppercase(string) Object Pascal (Delphi)
upcase(char) Object Pascal (Delphi) (operates on one character)
strtoupper(string) PHP
upper(string) Seed7
${string_param^^} (mnemonic: ^ is pointing up) Bash
echo "string" | tr 'a-z' 'A-Z' Unix
translate(string)
UPPER variables
PARSE UPPER VAR SrcVar DstVar
Rexx
string.upper() Python
upcase(string) Pick Basic
string.upcase Ruby[35]
strings.ToUpper(string) Go
(string-upcase string) Scheme, Common Lisp
String.uppercase string OCaml
String.map Char.toUpper string Standard ML
map Char.toUpper string Haskell
string.toUpperCase() Java, JavaScript
to_upper(string) Erlang
string.ToUpper() VB .NET, C#, Windows PowerShell, F#
string.uppercaseString Objective-C (NSString * only), Swift (Foundation)
string.upper(string)
(string):upper()
Lua
string asUppercase Smalltalk
UPPER(string) SQL
ToUpperCase[string] Mathematica
«FUNCTION» UPPER-CASE(string) COBOL
string.toUpper Cobra
string toupper string Tcl
string.to_uppercase() Rust[51]
// Example in C# "Wiki means fast?".ToUpper(); // "WIKI MEANS FAST?" 
# Example in Perl 5 uc("Wiki means fast?"); # "WIKI MEANS FAST?" 
# Example in Raku uc("Wiki means fast?"); # "WIKI MEANS FAST?" "Wiki means fast?".uc; # "WIKI MEANS FAST?" 
/* Example in Rexx */ translate("Wiki means fast?") /* "WIKI MEANS FAST?" */ /* Example #2 */ A='This is an example.' UPPER A /* "THIS IS AN EXAMPLE." */ /* Example #3 */ A='upper using Translate Function.' Translate UPPER VAR A Z /* Z="UPPER USING TRANSLATE FUNCTION." */ 
; Example in Scheme (use-modules (srfi srfi-13)) (string-upcase "Wiki means fast?") ; "WIKI MEANS FAST?" 
' Example in Visual Basic UCase("Wiki means fast?") ' "WIKI MEANS FAST?" 

trim edit

trim or strip is used to remove whitespace from the beginning, end, or both beginning and end, of a string.

Example usage Languages
String.Trim([chars]) C#, VB.NET, Windows PowerShell
string.strip(); D
(.trim string) Clojure
sequence [ predicate? ] trim Factor
(string-trim '(#\Space #\Tab #\Newline) string) Common Lisp
(string-trim string) Scheme
string.trim() Java, JavaScript (1.8.1+, Firefox 3.5+), Rust[52]
Trim(String) Pascal,[53] QBasic, Visual Basic, Delphi
string.strip() Python
strings.Trim(string, chars) Go
LTRIM(RTRIM(String)) Oracle SQL, T-SQL
strip(string [,option, char]) REXX
string:strip(string [,option, char]) Erlang
string.strip
string.lstrip
string.rstrip
Ruby
string.trim Raku
trim(string) PHP, Raku
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] Objective-C using Cocoa
string withBlanksTrimmed
string withoutSpaces
string withoutSeparators
Smalltalk (Squeak, Pharo)
Smalltalk
strip(string) SAS
string trim $string Tcl
TRIM(string)
TRIM(ADJUSTL(string))
Fortran
TRIM(string) SQL
TRIM(string)
LTrim(string)
RTrim(String)
ColdFusion
String.trim string OCaml 4+

Other languages

In languages without a built-in trim function, it is usually simple to create a custom function which accomplishes the same task.

APL edit

APL can use regular expressions directly:

Trim'^ +| +$'⎕R'' 

Alternatively, a functional approach combining Boolean masks that filter away leading and trailing spaces:

Trim{/⍨(\⌽∨\∘)' '} 

Or reverse and remove leading spaces, twice:

Trim{(\' ')/}2 

AWK edit

In AWK, one can use regular expressions to trim:

 ltrim(v) = gsub(/^[ \t]+/, "", v) rtrim(v) = gsub(/[ \t]+$/, "", v) trim(v) = ltrim(v); rtrim(v) 

or:

 function ltrim(s) { sub(/^[ \t]+/, "", s); return s } function rtrim(s) { sub(/[ \t]+$/, "", s); return s } function trim(s) { return rtrim(ltrim(s)); } 

C/C++ edit

There is no standard trim function in C or C++. Most of the available string libraries[54] for C contain code which implements trimming, or functions that significantly ease an efficient implementation. The function has also often been called EatWhitespace in some non-standard C libraries.

In C, programmers often combine a ltrim and rtrim to implement trim:

#include <string.h> #include <ctype.h> void rtrim(char *str) {  char *s;  s = str + strlen(str);  while (--s >= str) {  if (!isspace(*s)) break;  *s = 0;  } } void ltrim(char *str) {  size_t n;  n = 0;  while (str[n] != '\0' && isspace((unsigned char) str[n])) {  n++;  }  memmove(str, str + n, strlen(str) - n + 1); } void trim(char *str) {  rtrim(str);  ltrim(str); } 

The open source C++ library Boost has several trim variants, including a standard one:[55]

#include <boost/algorithm/string/trim.hpp> trimmed = boost::algorithm::trim_copy("string"); 

With boost's function named simply trim the input sequence is modified in-place, and returns no result.

Another open source C++ library Qt, has several trim variants, including a standard one:[56]

#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.[57]

Haskell edit

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 edit

The trim algorithm in J is a functional description:

 trim =. #~ [: (+./\ *. +./\.) ' '&~: 

That is: filter (#~) for non-space characters (' '&~:) between leading (+./\) and (*.) trailing (+./\.) spaces.

JavaScript edit

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 edit

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 edit

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 of 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 edit

XSLT includes the function normalize-space(string) which strips leading and trailing whitespace, in addition to replacing any whitespace sequence (including line breaks) with a single space.

Example:

<xsl:variable name='trimmed'>  <xsl:value-of select='normalize-space(string)'/> </xsl:variable> 

XSLT 2.0 includes regular expressions, providing another mechanism to perform string trimming.

Another XSLT technique for trimming is to utilize the XPath 2.0 substring() function.

References edit

  1. ^ a b c d e the index can be negative, which then indicates the number of places before the end of the string.
  2. ^ In Rust, the str::chars method iterates over code points and the std::iter::Iterator::nth method on iterators returns the zero-indexed nth value from the iterator, or None.
  3. ^ the index can not be negative, use *-N where N indicate the number of places before the end of the string.
  4. ^ In C++, the overloaded operator<=> method on a string returns a std::strong_ordering object (otherwise std::weak_ordering): less, equal (same as equivalent), or greater.
  5. ^ returns LESS, EQUAL, or GREATER
  6. ^ returns LT, EQ, or GT
  7. ^ returns .TRUE. or .FALSE.. These functions are based on the ASCII collating sequence.
  8. ^ a b IBM extension.
  9. ^ In Rust, the Ord::cmp method on a string returns an Ordering: Less, Equal, or Greater.
  10. ^ a b c d e f In Rust, the operators == and != and the methods eq, ne are implemented by the PartialEq trait, and the operators <, >, <=, >= and the methods lt, gt, le, ge are implemented by the PartialOrd trait.
  11. ^ The operators use the compiler's default collating sequence.
  12. ^ modifies string1, which must have enough space to store the result
  13. ^ In Rust, the + operator is implemented by the Add trait.
  14. ^ See the str::contains method.
  15. ^ See the std::basic_string::contains method.
  16. ^ a b startpos is IBM extension.
  17. ^ a b See the str::find method.
  18. ^ startpos is IBM extension.
  19. ^ "scan in Fortran Wiki". Fortranwiki.org. 2009-04-30. Retrieved 2013-08-18.
  20. ^ "verify in Fortran Wiki". Fortranwiki.org. 2012-05-03. Retrieved 2013-08-18.
  21. ^ formatstring must be a fixed literal at compile time for it to have the correct type.
  22. ^ See std::format, which is imported by the Rust prelude so that it can be used under the name format.
  23. ^ See the slice::join method.
  24. ^ if n is larger than the length of the string, then in Debug mode ArrayRangeException is thrown, in Release mode, the behaviour is unspecified.
  25. ^ if n is larger than the length of the string, Java will throw an IndexOutOfBoundsException
  26. ^ a b if n is larger than length of string, raises Invalid_argument
  27. ^ a b if n is larger than length of string, throw the message "StringTake::take:"
  28. ^ a b c In Rust, strings are indexed in terms of byte offsets and there is a runtime panic if the index is out of bounds or if it would result in invalid UTF-8. A &str (string reference) can be indexed by various types of ranges, including Range (0..n), RangeFrom (n..), and RangeTo (..n) because they all implement the SliceIndex trait with str being the type being indexed. The str::get method is the non-panicking way to index. It returns None in the cases in which indexing would panic.
  29. ^ Ruby lacks Unicode support
  30. ^ See the str::len method.
  31. ^ In Rust, the str::chars method iterates over code points and the std::iter::Iterator::count method on iterators consumes the iterator and returns the total number of elements in the iterator.
  32. ^ operates on one character
  33. ^ a b The transform function exists in the std:: namespace. You must include the <algorithm> header file to use it. The tolower and toupper functions are in the global namespace, obtained by the <ctype.h> header file. The std::tolower and std::toupper names are overloaded and cannot be passed to std::transform without a cast to resolve a function overloading ambiguity, e.g. std::transform(string.begin(), string.end(), result.begin(), (int (*)(int))std::tolower);
  34. ^ std::string only, result is stored in string result which is at least as long as string, and may or may not be string itself
  35. ^ a b only ASCII characters as Ruby lacks Unicode support
  36. ^ See the str::to_lowercase method.
  37. ^ See the str::replace method.
  38. ^ a b c d e The "find" string in this construct is interpreted as a regular expression. Certain characters have special meaning in regular expressions. If you want to find a string literally, you need to quote the special characters.
  39. ^ third parameter is non-standard
  40. ^ In Rust, the str::chars method iterates over code points, the std::iter::Iterator::rev method on reversible iterators (std::iter::DoubleEndedIterator) creates a reversed iterator, and the std::iter::Iterator::collect method consumes the iterator and creates a collection (which here is specified as a String with the turbofish syntax) from the iterator's elements.
  41. ^ See the str::rfind method.
  42. ^ . Es5.github.com. Archived from the original on 2013-01-28. Retrieved 2013-08-18.
  43. ^ if n is larger than length of string, then in Debug mode ArrayRangeException is thrown, and unspecified behaviour in Release mode
  44. ^ See the str::split and str::rsplit methods.
  45. ^ a b c d e f g startpos can be negative, which indicates to start that number of places before the end of the string.
  46. ^ a b numChars can be negative, which indicates to end that number of places before the end of the string.
  47. ^ startpos can not be negative, use * - startpos to indicate to start that number of places before the end of the string.
  48. ^ numChars can not be negative, use * - numChars to indicate to end that number of places before the end of the string.
  49. ^ a b c d e endpos can be negative, which indicates to end that number of places before the end of the string.
  50. ^ std::string only, result is stored in string result which is at least as long as string, and may or may not be string itself
  51. ^ In Rust, the str::to_uppercase method returns a newly allocated String with any lowercase characters changed to uppercase ones following the Unicode rules.
  52. ^ In Rust, the str::trim method returns a reference to the original &str.
  53. ^ "Trim – GNU Pascal priručnik". Gnu-pascal.de. Retrieved 2013-08-24.
  54. ^ "String library comparison". And.org. Retrieved 2013-08-24.
  55. ^ "Usage – 1.54.0". Boost.org. 2013-05-22. Retrieved 2013-08-24.
  56. ^ [1] August 2, 2009, at the Wayback Machine
  57. ^ dankamongmen. "sprezzos-kernel-packaging/changelog at master · dankamongmen/sprezzos-kernel-packaging · GitHub". Github.com. Retrieved 2016-05-29.

comparison, programming, languages, string, functions, string, functions, redirects, here, string, functions, formal, language, theory, string, operations, further, information, comparison, programming, languages, string, functions, used, computer, programming. String functions redirects here For string functions in formal language theory see String operations Further information Comparison of programming languages String functions are used in computer programming languages to manipulate a string or query information about a string some do both Most programming languages that have a string datatype will have some string functions although there may be other low level ways within each language to handle strings directly In object oriented languages string functions are often implemented as properties and methods of string objects In functional and list based languages a string is represented as a list of character codes therefore all list manipulation procedures could be considered string functions However such languages may implement a subset of explicit string specific functions as well For function that manipulate strings modern object oriented languages like C and Java have immutable strings and return a copy in newly allocated dynamic memory while others like C manipulate the original string unless the programmer copies data to a new string See for example Concatenation below The most basic example of a string function is the length string function This function returns the length of a string literal e g length hello world would return 11 Other languages may have string functions with similar or exactly the same syntax or parameters or outcomes For example in many languages the length function is usually represented as len string The below list of common functions aims to help limit this confusion Contents 1 Common string functions multi language reference 1 1 CharAt 1 2 Compare integer result 1 3 Compare relational operator based Boolean result 1 4 Concatenation 1 5 Contains 1 6 Equality 1 7 Find 1 8 Find character 1 9 Format 1 10 Inequality 1 11 index 1 12 indexof 1 13 instr 1 14 instrrev 1 15 join 1 16 lastindexof 1 17 left 1 18 len 1 19 length 1 20 locate 1 21 Lowercase 1 22 mid 1 23 partition 1 24 replace 1 25 reverse 1 26 rfind 1 27 right 1 28 rpartition 1 29 slice 1 30 split 1 31 sprintf 1 32 strip 1 33 strcmp 1 34 substring 1 35 Uppercase 1 36 trim 1 36 1 APL 1 36 2 AWK 1 36 3 C C 1 36 4 Haskell 1 36 5 J 1 36 6 JavaScript 1 36 7 Perl 1 36 8 Tcl 1 36 9 XSLT 2 ReferencesCommon string functions multi language reference editThis section does not cite any sources Please help improve this section by adding citations to reliable sources Unsourced material may be challenged and removed July 2013 Learn how and when to remove this template message String functions common to many languages are listed below including the different names used The below list of common functions aims to help programmers find the equivalent function in a language Note string concatenation and regular expressions are handled in separate pages Statements in guillemets are optional CharAt edit Definition charAt string integer returns character Description Returns character at index in the string Equivalent See substring of length 1 character Format Languages Base index i string i i i i ALGOL 68 APL Julia Pascal Object Pascal Delphi Seed7 1 i string i i i i C C C Cobra D FreeBASIC Go Python 1 PHP Ruby 1 Windows PowerShell JavaScript APL 0 i string i i i i PHP deprecated in 5 3 0 i string i i i i Ada 1Mid i string i i 1 VB 1MID i string i i 1 BASIC 1 i string i Chars i i i VB NET 0 i string i i i i i i i Fortran 1 i string i charAt i i i Java JavaScript 0 i string i i i i OCaml F 0 i string i chars nth i i i Rust 2 0 i string i i i i i 1 i Pick Basic 1String sub i string i i i i Standard ML 0 i string i i i i Haskell 0 string ref i string i i i i Scheme 0 char i string i i i i Common Lisp 0 elt i string i i i i ISLISP 0 get i string i i i i Clojure 0substr i string i i i i 1 Perl 5 1 0substr i string i i i i 1 string substr i i i 1 Raku 3 0substr i string i i i i 1 PL I 1 i string i at i i i C STL w bounds checking 0lists nth i i i i string i Erlang 1 i string i characterAtIndex i i i Objective C NSString only 0string sub i string i i i i i i i i string i sub i i i i i i Lua 1 1 i string i at i i i Smalltalk w bounds checking 1string index i string i i Tcl 0StringTake i string i i i i Mathematica Wolfram Language 1 1 i string i i i i Eiffel 1 i string i i i i 1 COBOL 1 i string param i i i i 1 Bash 0 i i i i string i APL 0 or 1 Example in Pascal var MyStr string Hello World MyChar Char begin MyChar MyStr 2 e Example in ALGOL 68 Hello World 2 e Example in C include lt stdio h gt for printf char MyStr Hello World printf c MyStr 1 e printf c MyStr 7 W printf c MyStr 11 d printf s MyStr Hello World printf s Hello 2 World 2 Hello 2 World 2 Example in C include lt iostream gt for cout include lt string h gt for string data type using namespace std char MyStr1 Hello 1 World 1 string MyStr2 Hello 2 World 2 cout lt lt Hello 3 World 3 Hello 3 World 3 cout lt lt MyStr2 6 2 cout lt lt MyStr1 substr 5 3 1 Example in C Hello World 2 l Example in Perl 5 substr Hello World 1 1 e Examples in Python Hello World 2 l Hello World 3 r Example in Raku Hello World substr 1 1 e Example in Visual Basic Mid Hello World 2 1 Example in Visual Basic NET Hello World Chars 2 l c Example in Smalltalk Hello World at 2 e Example in Rust Hello World chars nth 2 Some l Compare integer result edit Definition compare string sub 1 sub string sub 2 sub returns integer Description Compares two strings to each other If they are equivalent a zero is returned Otherwise most of these routines will return a positive or negative result corresponding to whether string1 is lexicographically greater than or less than respectively than string2 The exceptions are the Scheme and Rexx routines which return the index of the first mismatch and Smalltalk which answer a comparison code telling how the receiver sorts relative to string parameter Format LanguagesIF i string sub 1 sub i lt i string sub 2 sub i THEN 1 ELSE ABS i string sub 1 sub i gt i string sub 2 sub i FI ALGOL 68cmp i string sub 1 sub i i string sub 2 sub i Python 2 i string sub 1 sub i gt i string sub 2 sub i i string sub 1 sub i lt i string sub 2 sub i Python a href Strcmp html class mw redirect title Strcmp strcmp a i string sub 1 sub i i string sub 2 sub i C PHPstd string cmp i string sub 1 sub i i string sub 2 sub i DStrComp i string sub 1 sub i i string sub 2 sub i VB Object Pascal Delphi i string sub 1 sub i cmp i string sub 2 sub i Perl Raku i string sub 1 sub i compare i string sub 2 sub i Smalltalk Squeak Pharo i string sub 1 sub i lt gt i string sub 2 sub i Ruby C STL C 20 4 i string sub 1 sub i compare i string sub 2 sub i C STL Swift Foundation compare i string sub 1 sub i i string sub 2 sub i Rexx Seed7CompareStr i string sub 1 sub i i string sub 2 sub i Pascal Object Pascal Delphi i string sub 1 sub i compareTo i string sub 2 sub i Cobra Java i string sub 1 sub i CompareTo i string sub 2 sub i VB NET C F compare i string sub 1 sub i i string sub 2 sub i Clojure string i string sub 1 sub i i string sub 2 sub i Common Lisp string compare i string sub 1 sub i i string sub 2 sub i i p lt i i p i i p gt i Scheme SRFI 13 string i string sub 1 sub i i string sub 2 sub i ISLISPcompare i string sub 1 sub i i string sub 2 sub i OCamlString compare i string sub 1 sub i i string sub 2 sub i Standard ML 5 compare i string sub 1 sub i i string sub 2 sub i Haskell 6 string Compare i string sub 1 sub i i string sub 2 sub i Windows PowerShell i string sub 1 sub i compare i string sub 2 sub i Objective C NSString only LLT i string sub 1 sub i i string sub 2 sub i LLE i string sub 1 sub i i string sub 2 sub i LGT i string sub 1 sub i i string sub 2 sub i LGE i string sub 1 sub i i string sub 2 sub i Fortran 7 i string sub 1 sub i localeCompare i string sub 2 sub i JavaScriptbytes Compare byte i string sub 1 sub i byte i string sub 2 sub i Gostring compare i string sub 1 sub i i string sub 2 sub i Tclcompare i string sub 1 sub i i string sub 2 sub i i count i PL I 8 i string sub 1 sub i cmp i string sub 2 sub i Rust 9 Example in Perl 5 hello cmp world returns 1 Example in Python cmp hello world returns 1 Examples in Raku hello cmp world returns Less world cmp hello returns More hello cmp hello returns Same Example in Rexx compare hello world returns index of mismatch 1 Example in Scheme use modules srfi srfi 13 returns index of mismatch 0 string compare hello world values values values Compare relational operator based Boolean result edit Definition string sub 1 sub OP string sub 2 sub OR compare string sub 1 sub string sub 2 sub returns Boolean Description Lexicographically compares two strings using a relational operator or function Boolean result returned Format Languages i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt gt lt and gt Pascal Object Pascal Delphi OCaml Seed7 Standard ML BASIC VB VB NET F i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt and Also EQ NE LT LE GE and GT ALGOL 68 stringOP i string sub 1 sub i i string sub 2 sub i where OP can be any of ci lt ci lt gt ci gt lt ci lt gt and ci gt operators starting with ci are case insensitive Scheme stringOP i string sub 1 sub i i string sub 2 sub i where OP can be any of ci lt gt ci lt gt lt ci lt gt ci gt lt ci lt gt and ci gt operators starting with ci are case insensitive Scheme SRFI 13 stringOP i string sub 1 sub i i string sub 2 sub i where OP can be any of equal not equal lt lessp gt greaterp lt not greaterp gt and not lessp the verbal operators are case insensitive Common Lisp stringOP i string sub 1 sub i i string sub 2 sub i where OP can be any of lt gt lt and gt ISLISP i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt and gt Rexx i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt gt lt and gt PL I i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt and gt Ada i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt and gt Erlang i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt and gt Haskell i string sub 1 sub i OP i string sub 2 sub i where OP can be any of i eq i i ne i i lt i i gt i i le i and i ge i Perl Raku i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt and gt C STL C D Go JavaScript Python PHP Ruby Rust 10 Swift i string sub 1 sub i OP i string sub 2 sub i where OP can be any of eq ceq ne cne lt clt gt cgt le cle ge and cge operators starting with c are case sensitive Windows PowerShell i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt and gt Lua i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt and gt Smalltalk i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt and gt Also EQ NE LT LE GT and GE Fortran 11 i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt gt lt gt as well as worded equivalents COBOL i string sub 1 sub i OP i string sub 2 sub i where OP can be any of lt gt lt gt lt and gt Cobra i string sub 1 sub i OP i string sub 2 sub i is available in the syntax but means comparison of the pointers pointing to the strings not of the string contents Use the Compare integer result function C Java i string sub 1 sub i METHOD i string sub 2 sub i where METHOD is any of eq ne gt lt ge le Rust 10 Example in Erlang hello gt world returns false Example in Raku art gt painting returns False art lt painting returns True Example in Windows PowerShell hello gt world returns false Example in Common Lisp string gt art painting returns nil string lt art painting returns non nil Concatenation edit See also Concatenation Definition concatenate string sub 1 sub string sub 2 sub returns string Description Concatenates joins two strings to each other returning the combined string Note that some languages like C have mutable strings so really the second string is being appended to the first string and the mutated string is returned Format Languages i string sub 1 sub i amp i string sub 2 sub i Ada FreeBASIC Seed7 BASIC VB VB NET COBOL between literals only a href Strcat html class mw redirect title Strcat strcat a i string sub 1 sub i i string sub 2 sub i C C a href Character computing html title Character computing char a a href Pointer computer programming html title Pointer computer programming a only 12 i string sub 1 sub i i string sub 2 sub i Perl PHP i string sub 1 sub i i string sub 2 sub i ALGOL 68 C STL C Cobra FreeBASIC Go Pascal Object Pascal Delphi Java JavaScript Windows PowerShell Python Ruby Rust 13 F Swift Turing VB i string sub 1 sub i i string sub 2 sub i D Raku string append i string sub 1 sub i i string sub 2 sub i Scheme ISLISP concatenate string i string sub 1 sub i i string sub 2 sub i Common Lisp str i string sub 1 sub i i string sub 2 sub i Clojure i string sub 1 sub i i string sub 2 sub i Rexx SQL PL I i string sub 1 sub i i string sub 2 sub i Fortran i string sub 1 sub i i string sub 2 sub i Erlang Haskell i string sub 1 sub i i string sub 2 sub i OCaml Standard ML F i string sub 1 sub i stringByAppendingString i string sub 2 sub i Objective C NSString only i string sub 1 sub i i string sub 2 sub i Lua i string sub 1 sub i i string sub 2 sub i Smalltalk APL i string sub 1 sub i i string sub 2 sub i SNOBOL i string sub 1 sub string sub 2 sub i Bash i string sub 1 sub i lt gt i string sub 2 sub i Mathematicaconcat string1 string2 Tcl Example in Pascal abc def returns abcdef Example in C abc def returns abcdef Example in Visual Basic abc amp def returns abcdef abc def returns abcdef abc amp Null returns abc abc Null returns Null Example in D abc def returns abcdef Example in common lisp concatenate string abc def ghi returns abc def ghi Example in Perl 5 abc def returns abcdef Perl 5 returns Perl 5 Example in Raku abc def returns abcdef Perl 6 returns Perl 6 Contains edit Definition contains i string i i substring i returns booleanDescription Returns whether string contains substring as a substring This is equivalent to using Find and then detecting that it does not result in the failure condition listed in the third column of the Find section However some languages have a simpler way of expressing this test Related FindFormat Languages i string in string i i string i b loc int b i substring i ALGOL 68 i ContainsStr i i string i i substring i Object Pascal Delphi strstr i string i i substring i NULL C C char only i string i Contains i substring i C VB NET Windows PowerShell F i string i contains i substring i Cobra Java 1 5 Raku Rust 14 C C 23 15 i string i indexOf i substring i gt 0 JavaScriptstrpos i string i i substring i false PHPstr contains i string i i substring i PHP 8 pos i string i i substring i lt gt 0 Seed7 i substring i in i string i Cobra Python 2 3 string find i string i i substring i nil Lua i string i include i substring i RubyData List isInfixOf i substring i i string i Haskell GHC 6 6 i string i includesSubstring i substring i Smalltalk Squeak Pharo Smalltalk X String isSubstring i substring i i string i Standard ML search i substring i i string i Common Lisp not null string index i substring i i string i ISLISP substring i substring i i string i Clojure StringFreeQ i string i i substring i Mathematicaindex i string i i substring i i startpos i gt 0 Fortran PL I 16 index i string i i substring i i occurrence i gt 0 Pick Basicstrings Contains i string i i substring i Go i string i find i substring i string npos C i string i containsString i substring i Objective C NSString only iOS 8 OS X 10 10 i string i rangeOfString i substring i nil Swift Foundation i substring i i string i APL Example in ALGOL 68 string in string e loc int Hello mate returns true string in string z loc int word returns false Example In C Hello mate Contains e returns true word Contains z returns false Example in Python e in Hello mate returns true z in word returns false Example in Raku Good morning contains z returns False Buenos dias contains i returns True Example in Smalltalk Hello mate includesSubstring e returns true word includesSubstring z returns false Equality edit Tests if two strings are equal See also Compare and Compare Note that doing equality checks via a generic Compare with integer result is not only confusing for the programmer but is often a significantly more expensive operation this is especially true when using C strings Format Languages i string sub 1 sub i i string sub 2 sub i Python C STL C Cobra Go JavaScript similarity PHP similarity Ruby Rust 10 Erlang Haskell Lua D Mathematica Swift i string sub 1 sub i i string sub 2 sub i JavaScript PHP i string sub 1 sub i i string sub 2 sub i i string sub 1 sub i EQ i string sub 2 sub i Fortranstrcmp i string sub 1 sub i i string sub 2 sub i 0 C string i string sub 1 sub i i string sub 2 sub i Scheme string i string sub 1 sub i i string sub 2 sub i Common Lisp ISLISP i string sub 1 sub i i string sub 2 sub i ALGOL 68 Ada Object Pascal Delphi OCaml Pascal Rexx Seed7 Standard ML BASIC VB VB NET F Smalltalk PL I COBOLtest i string sub 1 sub i i string sub 2 sub i i string sub 1 sub i i string sub 2 sub i Bourne Shell i string sub 1 sub i eq i string sub 2 sub i Perl Raku i string sub 1 sub i equals i string sub 2 sub i Cobra Java i string sub 1 sub i Equals i string sub 2 sub i C i string sub 1 sub i eq i string sub 2 sub i string Equals i string sub 1 sub i i string sub 2 sub i Windows PowerShell i string sub 1 sub i isEqualToString i string sub 2 sub i i string sub 1 sub i isEqual i string sub 2 sub i Objective C NSString only i string sub 1 sub i i string sub 2 sub i APL i string sub 1 sub i eq i string sub 2 sub i Rust 10 Example in C hello world returns false Example in Visual Basic hello world returns false Examples in Perl 5 hello eq world returns 0 hello eq hello returns 1 Examples in Raku hello eq world returns False hello eq hello returns True Example in Windows PowerShell hello eq world returns false Example in APL hello world returns 0 Find edit Definition find i string i i substring i returns integerDescription Returns the position of the start of the first occurrence of substring in string If the substring is not found most of these routines return an invalid index value 1 where indexes are 0 based 0 where they are 1 based or some value to be interpreted as Boolean FALSE Related instrrevFormat Languages If not foundstring in string substring pos i string startpos i ALGOL 68 returns BOOL TRUE or FALSE and position in REF INT pos InStr i startpos i i string i i substring i VB positions start at 1 returns 0INSTR i string i i substring i BASIC positions start at 1 returns 0index i string i i substring i AWK returns 0index i string i i substring i i startpos i Perl 5 returns 1index i string i i substring i i startpos i i string i index i substring i i startpos i Raku returns Nilinstr i startpos i i string i i substring i FreeBASIC returns 0strpos i string i i substring i i startpos i PHP returns FALSElocate i string i i substring i Ingres returns string length 1strstr i string i i substring i C C a href Character computing html title Character computing char a a href Pointer computer programming html title Pointer computer programming a only returns pointer to first character returns NULLstd string indexOf i string i i substring i D returns 1pos i string i i substring i i startpos i Seed7 returns 0strings Index i string i i substring i Go returns 1pos i substring i i string i Pascal Object Pascal Delphi returns 0pos i substring i i string i i startpos i Rexx returns 0 i string i find i substring i i startpos i C STL returns std string npos i string i find i substring i i startpos i i endpos i Python returns 1 i string i index i substring i i startpos i i endpos i raises ValueError i string i index i substring i i startpos i Ruby returns nil i string i indexOf i substring i i startpos i Java JavaScript returns 1 i string i IndexOf i substring i i startpos i i charcount i VB NET C Windows PowerShell F returns 1string str i string i i substring i Erlang returns 0 string contains i string i i substring i Scheme SRFI 13 returns f search i substring i i string i Common Lisp returns NIL string index i substring i i string i ISLISP returns nilList findIndex List isPrefixOf i substring i List tails i string i Haskell returns only index returns NothingStr search forward Str regexp string i substring i i string i 0 OCaml raises Not foundSubstring size 1 Substring position i substring i Substring full i string i Standard ML returns string length i string i rangeOfString i substring i location Objective C NSString only returns NSNotFoundstring find i string i i substring i i string i find i substring i Lua returns nil i string i indexOfSubCollection i substring i startingAt i startpos i ifAbsent i aBlock i i string i findString i substring i startingAt i startpos i Smalltalk Squeak Pharo evaluate aBlock which is a block closure or any object understanding value returns 0startpos INDEX i string i i substring i i back i i kind i Fortran returns 0 if substring is not in string returns LEN string 1 if substring is emptyPOSITION i substring i IN i string i SQL returns 0 positions start at 1 index i string i i substring i i startpos i PL I 16 returns 0 positions start at 1 index i string i i substring i i occurrence i Pick Basic returns 0 if occurrence of substring is not in string positions start at 1 i string i indexOf i substring i i startpos i i charcount i Cobra returns 1string first i substring string startpos i Tcl returns 1 i substring i i string i 1 APL returns 1 the last position in string i string i find i substring i Rust 17 returns NoneExamples Common Lisp search e Hello mate returns 1 search z word returns NIL C Hello mate IndexOf e returns 1 Hello mate IndexOf e 4 returns 9 word IndexOf z returns 1 Raku Hello there index e returns 1 Hello there index z returns Nil Scheme use modules srfi srfi 13 string contains Hello mate e returns 1 string contains word z returns f Visual Basic Examples in InStr Hello mate e returns 2 InStr 5 Hello mate e returns 10 InStr word z returns 0 Smalltalk Hello mate indexOfSubCollection ate returns 8 Hello mate indexOfSubCollection late returns 0 I Hello mate indexOfSubCollection late ifAbsent 99 returns 99 Hello mate indexOfSubCollection late ifAbsent self error raises an exception Find character edit Definition find character i string i i char i returns integerDescription Returns the position of the start of the first occurrence of the character char in string If the character is not found most of these routines return an invalid index value 1 where indexes are 0 based 0 where they are 1 based or some value to be interpreted as Boolean FALSE This can be accomplished as a special case of Find with a string of one character but it may be simpler or more efficient in many languages to locate just one character Also in many languages characters and strings are different types so it is convenient to have such a function Related findFormat Languages If not foundchar in string char pos i string startpos i ALGOL 68 returns BOOL TRUE or FALSE and position in REF INT pos instr i string i any i char i i startpos i char can contain more them one char in which case the position of the first appearance of any of them is returned FreeBASIC returns 0strchr i string i i char i C C a href Character computing html title Character computing char a a href Pointer computer programming html title Pointer computer programming a only returns pointer to character returns NULLstd string find i string i i dchar i D returns 1 i string i find i char i i startpos i C STL returns std string npospos i string i i char i i startpos i Seed7 returns 0strings IndexRune i string i i char i Go returns 1 i string i indexOf i char i i startpos i Java JavaScript returns 1 i string i IndexOf i char i i startpos i i charcount i VB NET C Windows PowerShell F returns 1 position i char i i string i Common Lisp returns NIL char index i char i i string i ISLISP returns nilList elemIndex i char i i string i Haskell returns Just i index i returns NothingString index i string i i char i OCaml raises Not foundposition SCAN i string i i set i i back i i kind i position VERIFY i string i i set i i back i i kind i sup class reference plainlinks nourlexpansion id ref Fortran find a href endnote Fortran find a a sup Fortran returns zero i string i indexOf i char i ifAbsent aBlock i string i indexOf i char i i string i includes i char i Smalltalk evaluate aBlock which is a BlockClosure or any object understanding value returns 0returns true or falseindex i string i i char i i startpos i PL I 18 returns 0 positions start at 1 i string i index i char i Ruby returns nilstrpos i string i i char i i startpos i PHP returns false i string i indexOf i char i i startpos i i charcount i Cobra returns 1 i string i i char i APL returns 1 the last position in string i string i find i substring i Rust 17 returns None Examples in C Hello mate IndexOf e returns 1 word IndexOf z returns 1 Examples in Common Lisp position e Hello mate returns 1 position z word returns NIL a Given a set of characters SCAN returns the position of the first character found 19 while VERIFY returns the position of the first character that does not belong to the set 20 Format edit See also printf format string Definition format i formatstring i i items i returns stringDescription Returns the formatted string representation of one or more items Format Languages Format string syntaxassociate i file i i string i putf i file i i formatstring i i items i ALGOL 68 ALGOLFormat i item i i formatstring i VB a href Sprintf html class mw redirect title Sprintf sprintf a i formatstring i i items i Perl PHP Raku Ruby Citem fmt i formatstring i Raku Cio lib format i formatstring i i items i Erlang a href Sprintf html class mw redirect title Sprintf sprintf a i outputstring i i formatstring i i items i C Cstd format i formatstring i i items i C C 20 Pythonstd string format i formatstring i i items i D CFormat i formatstring i i items i Object Pascal Delphi fmt Sprintf i formatstring i i items i Go C a href Printf Unix html title Printf Unix printf a i formatstring i i items i Unix C i formatstring i i items i Python Ruby C i formatstring i format i items i Python NETf i formatstring i Python 3Printf sprintf i formatstring i 21 i items i OCaml F CText Printf printf i formatstring i i items i Haskell GHC C i formatstring i printf i items i Smalltalk CString format i formatstring i i items i Java CString Format i formatstring i i items i VB NET C F NET format i formatstring i i items i Scheme SRFI 28 Lisp a href Format Common Lisp html title Format Common Lisp format a nil i formatstring i i items i Common Lisp Lisp format i formatstring i i items i Clojure Lisp i formatstring i f i items i Windows PowerShell NET NSString stringWithFormat i formatstring i i items i Objective C NSString only CString format i formatstring i i items i Swift Foundation Cstring format i formatstring i i items i i formatstring i format i items i Lua CWRITE i outputstring i i formatstring i i items i Fortran Fortranput string i string i edit i items i i format i PL I PL I similar to Fortran String format i formatstring i i items i Cobra NETformat i formatstring items i Tcl C i formatnumbers i i items i i formatstring i FMT i items i APL APLformat i formatstring i i items i Rust 22 Python Example in C String Format My 0 costs 1 C2 pen 19 99 returns My pen costs 19 99 Example in Object Pascal Delphi Format My s costs 2f pen 1 9 99 returns My pen costs 19 99 Example in Java String format My s costs 2f pen 19 99 returns My pen costs 19 99 Examples in Raku sprintf My s costs 2f pen 19 99 returns My pen costs 19 99 1 fmt 04d returns 0001 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 Example in Python 3 6 pen pen f My pen costs 19 99 returns My pen costs 19 99 Example in Scheme format My a costs 1 2F pen 19 99 returns My pen costs 19 99 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 Inequality edit Tests if two strings are not equal See also Equality Format Languages i string sub 1 sub i b ne b i string sub 2 sub i i string sub 1 sub i NE i string sub 2 sub i ALGOL 68 note the operator ne is literally in bold type font i string sub 1 sub i i string sub 2 sub i ALGOL 68 Ada Erlang Fortran Haskell i string sub 1 sub i lt gt i string sub 2 sub i BASIC VB VB NET Pascal Object Pascal Delphi OCaml PHP Seed7 Standard ML F COBOL Cobra Python 2 deprecated i string sub 1 sub i i string sub 2 sub i BASIC some implementations i string sub 1 sub i ne i string sub 2 sub i Perl Raku string lt gt i string sub 1 sub i i string sub 2 sub i Scheme SRFI 13 string i string sub 1 sub i i string sub 2 sub i Common Lisp string i string sub 1 sub i i string sub 2 sub i ISLISP not i string sub 1 sub i i string sub 2 sub i Clojure i string sub 1 sub i i string sub 2 sub i C STL C Go JavaScript not similar PHP not similar Python Ruby Rust 10 Swift D Mathematica i string sub 1 sub i i string sub 2 sub i JavaScript PHP i string sub 1 sub i i string sub 2 sub i Rexx i string sub 1 sub i i string sub 2 sub i PL Itest i string sub 1 sub i i string sub 2 sub i i string sub 1 sub i i string sub 2 sub i Bourne Shell i string sub 1 sub i ne i string sub 2 sub i not string Equals i string sub 1 sub i i string sub 2 sub i Windows PowerShell i string sub 1 sub i i string sub 2 sub i Lua Smalltalk i string sub 1 sub i i string sub 2 sub i APL i string sub 1 sub i ne i string sub 2 sub i Rust 10 Example in C hello world returns true Example in Visual Basic hello lt gt world returns true Example in Clojure not hello world true Example in Perl 5 hello ne world returns 1 Example in Raku hello ne world returns True Example in Windows PowerShell hello ne world returns true index edit see Find indexof edit see Find instr edit see Find instrrev edit see rfind join edit Definition join i separator i i list of strings i returns a list of strings joined with a separatorDescription Joins the list of strings into a new string with the separator string between each of the substrings Opposite of split Related sprintfFormat Languagesstd string join i array of strings i i separator i Dstring join i list of strings i i separator i Erlangjoin i separator i i list of strings i Perl PHP Rakuimplode i separator i i array of strings i PHP i separator i join i sequence of strings i Python Swift 1 x i array of strings i join i separator i Ruby JavaScript Raku Rust 23 string join i array of strings i i separator i Scheme SRFI 13 a href Format Common Lisp html title Format Common Lisp format a nil a i separator i i array of strings i Common Lisp clojure string join i separator i i list of strings i apply str interpose i separator i i list of strings i Clojurestrings Join i array of strings i i separator i Gojoin i array of strings i i separator i Seed7String concat i separator i i list of strings i OCamlString concatWith i separator i i list of strings i Standard MLData List intercalate i separator i i list of strings i Haskell GHC 6 8 Join i array of strings i i separator i VBString Join i separator i i array of strings i VB NET C F String join i separator i i array of strings i Java 8 amp OFS i separator i i array of strings i i array of strings i join i separator i Windows PowerShell i array of strings i componentsJoinedByString i separator i Objective C NSString only table concat i table of strings i i separator i Lua String streamContents stream i collectionOfAnything i asStringOn stream delimiter i separator i i collectionOfAnything i joinUsing i separator i Smalltalk Squeak Pharo i array of strings i join i separator i i final separator i Cobra i sequence of strings i joinWithSeparator i separator i Swift 2 x1 i separator i i list of strings i APL Example in C String Join a b c a b c Example in Smalltalk a b c joinUsing a b c Example in Perl 5 join a b c a b c Example in Raku lt a b c gt join a b c Example in Python join a b c a b c Example in Ruby a b c join a b c Example in Scheme use modules srfi srfi 13 string join a b c a b c lastindexof edit see rfind left edit Definition left i string i i n i returns stringDescription Returns the left n part of a string If n is greater than the length of the string then most implementations return the whole string exceptions exist see code examples Note that for variable length encodings such as UTF 8 UTF 16 or Shift JIS it can be necessary to remove string positions at the end in order to avoid invalid strings Format Languagesstring string First string First i n i 1 Adasubstr i string i 0 i n i AWK changes string Perl PHP RakuLEFT i string i i n i BASIC VBleft i string i i n i VB FreeBASIC Ingres Pick Basicstrncpy i string2 i i string i i n i C standard library i string i substr 0 i n i C STL Raku i string i substringToIndex i n i Objective C NSString only apply str take i n i i string i Clojure i string i 0 i n i D 24 string substr i string i i start i i length i Erlang subseq i string i 0 i n i Common Lisp i string i i n i Cobra Go Pythonleft i string i i n i i padchar i Rexx Erlang i string i 0 i n i i string i 0 i n i 1 Ruby i string i 1 i n i Pick Basic i string i i n i Seed7 i string i Substring 0 i n i VB NET C Windows PowerShell F leftstr i string i i n i Pascal Object Pascal Delphi i copy i i string i 1 i n i Turbo Pascal i string i substring 0 i n i Java 25 JavaScript string take i string i i n i Scheme SRFI 13 take i n i i string i HaskellString extract i string i i n i NONE Standard MLString sub i string i 0 i n i OCaml 26 i string i i n i F string sub i string i 1 i n i i string i sub 1 i n i Lua i string i first i n i Smalltalk Squeak Pharo i string i i n i FortranStringTake i string i i n i Mathematica 27 i string i FUNCTION LENGTH i string i i n i i n i COBOL i string i substring 0 i n i Cobra i n i i string i APL i string i 0 n i string i n i string i get 0 n i string i get n Rust 28 Example in Raku Hello there substr 0 6 returns Hello Examples in Rexx left abcde 3 returns abc left abcde 8 returns abcde left abcde 8 returns abcde Examples in Scheme use modules srfi srfi 13 string take abcde 3 returns abc string take abcde 8 error Examples in Visual Basic Left sandroguidi 3 returns san Left sandroguidi 100 returns sandroguidi len edit see length length edit Definition length i string i returns an integer numberDescription Returns the length of a string not counting the null terminator or any other of the string s internal structural information An empty string returns a length of 0 Format Returns Languagesstring Length AdaUPB string ALGOL 68echo i string param i Bashlength i string i Ingres Perl 5 Pascal Object Pascal Delphi Rexx Seed7 SQL PL Ilen i string i BASIC FreeBASIC Python Go Pick Basiclength i string i string len i string i ErlangLen i string i VB Pick Basic i string i Length Number of UTF 16 code units VB NET C Windows PowerShell F chars i string i i string i chars Number of graphemes NFG Rakucodes i string i i string i codes Number of Unicode code points Raku i string i size OR i string i length Number of bytes 29 Ruby a href Strlen html class mw redirect title Strlen strlen a i string i Number of bytes C PHP i string i length C STL i string i length Cobra D JavaScript i string i length Number of UTF 16 code units Java string length i string i Scheme length i string i Common Lisp ISLISP count i string i ClojureString length i string i OCamlsize i string i Standard MLlength i string i Number of Unicode code points Haskell i string i length Number of UTF 16 code units Objective C NSString only i string i characters count Number of characters Swift 2 x count i string i Number of characters Swift 1 2 countElements i string i Number of characters Swift 1 0 1 1 string len i string i i string i len i string i Lua i string i size SmalltalkLEN i string i LEN TRIM i string i FortranStringLength i string i Mathematica FUNCTION LENGTH i string i or FUNCTION BYTE LENGTH i string i number of characters and number of bytes respectively COBOLstring length i string i a decimal string giving the number of characters Tcl i string i APL i string i len Number of bytes Rust 30 i string i chars count Number of Unicode code points Rust 31 Examples in C hello Length returns 5 Length returns 0 Examples in Erlang string len hello returns 5 string len returns 0 Examples in Perl 5 length hello returns 5 length returns 0 Examples in Raku chars chars both return 1 codes codes both return 4 chars chars both return 0 codes codes both return 0 Examples in Visual Basic Len hello returns 5 Len returns 0 Examples in Objective C hello Length returns 5 Length returns 0 Examples in Lua hello len returns 5 returns 0 locate edit see Find Lowercase edit Definition lowercase i string i returns stringDescription Returns the string in lower case Format LanguagesLCase i string i VBlcase i string i FreeBASIClc i string i Perl Raku i string i lc Rakutolower i char i C 32 std string toLower i string i Dtransform i string i begin i string i end i result i begin tolower 33 C 34 lowercase i string i Object Pascal Delphi strtolower i string i PHPlower i string i Seed7 i string param i Bash a href Echo command html title Echo command echo a string a href Tr program html class mw redirect title Tr program tr a A Z a z Unix i string i lower Pythondowncase i string i Pick Basic i string i downcase Ruby 35 strings ToLower i string i Go string downcase i string i Scheme R6RS Common Lisp lower case i string i ClojureString lowercase i string i OCamlString map Char toLower i string i Standard MLmap Char toLower i string i Haskell i string i toLowerCase Java JavaScriptto lower i string i Erlang i string i ToLower VB NET C Windows PowerShell F i string i lowercaseString Objective C NSString only Swift Foundation string lower i string i i string i lower Lua i string i asLowercase SmalltalkLOWER i string i SQLlowercase i string i PL I 8 ToLowerCase i string i Mathematica FUNCTION LOWER CASE i string i COBOL i string i toLower Cobrastring tolower i string i Tcl i string i to lowercase Rust 36 Example in C Wiki means fast ToLower wiki means fast Example in Scheme use modules srfi srfi 13 string downcase Wiki means fast wiki means fast Example in C include lt ctype h gt include lt stdio h gt int main void char string Wiki means fast int i for i 0 i lt sizeof string 1 i transform characters in place one by one string i tolower string i puts string wiki means fast return 0 Example in Raku Wiki means fast lc wiki means fast mid edit see substring partition edit Definition lt string gt partition separator returns the sub string before the separator the separator then the sub string after the separator Description Splits the given string by the separator and returns the three substrings that together make the original Format Languages Comments i string i partition i separator i Python Ruby 1 9 lists partition i pred i i string i Erlangsplit i separator i i string i 2 Perl 5split i separator i i string i 2 i string i split i separator i 2 Raku Separator does not have to be a regular expression 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 Examples in Perl 5 Raku 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 replace edit Definition replace i string i i find i i replace i returns stringDescription Returns a string with find occurrences changed to replace Format Languageschangestr i find i i string i i replace i Rexxstd string replace i string i i find i i replace i DReplace i string i i find i i replace i VBreplace i string i i find i i replace i Seed7change i string i i find i i replace i Pick Basic i string i Replace i find i i replace i C F VB NETstr replace i find i i replace i i string i PHPre replace i string i i find i i replace i return list Erlang i string i replace i find i i replace i Cobra Java 1 5 Python Rust 37 i string i replaceAll i find regex i i replace i sup id cite ref regex 38 0 class reference a href cite note regex 38 38 a sup Java i string i gsub i find i i replace i Ruby i string i s i find regex i i replace i g sup id cite ref regex 38 1 class reference a href cite note regex 38 38 a sup Perl 5 i string i subst i find i i replace i g Raku i string i replace i find i i replace i g sup id cite ref 39 class reference a href cite note 39 39 a sup i string i replace i find regex i g i replace i sup id cite ref regex 38 2 class reference a href cite note regex 38 38 a sup JavaScript a href Echo command html title Echo command echo a i string i a href Sed html title Sed sed a s i find regex i i replace i g sup id cite ref regex 38 3 class reference a href cite note regex 38 38 a sup Unix i string param i i find pattern i i replace i Bash i string i replace i find i i replace i i string i replace i find regex i i replace i sup id cite ref regex 38 4 class reference a href cite note regex 38 38 a sup Windows PowerShellStr global replace Str regexp string i find i i replace i i string i OCaml i string i stringByReplacingOccurrencesOfString i find i withString i replace i Objective C NSString only i string i stringByReplacingOccurrencesOfString i find i withString i replace i Swift Foundation string gsub i string i i find i i replace i i string i gsub i find i i replace i Lua i string i copyReplaceAll i find i with i replace i Smalltalk Squeak Pharo string map i find i i replace i i string i TclStringReplace i string i i find i gt i replace i Mathematicastrings Replace i string i i find i i replace i 1 GoINSPECT i string i REPLACING ALL LEADING FIRST i find i BY i replace i COBOL i find regex i R i replace regex i i string i APL Examples in C effffff Replace f jump returns ejumpjumpjumpjumpjumpjump blah Replace z y returns blah Examples in Java effffff replace f jump returns ejumpjumpjumpjumpjumpjump effffff replaceAll f jump returns ejump Examples in Raku effffff subst f jump g returns ejumpjumpjumpjumpjumpjump blah subst z y g returns blah Examples in Visual Basic Replace effffff f jump returns ejumpjumpjumpjumpjumpjump Replace blah z y returns blah Examples in Windows PowerShell effffff replace f jump returns ejumpjumpjumpjumpjumpjump effffff replace f jump returns ejump reverse edit Definition reverse i string i Description Reverses the order of the characters in the string Format Languagesreverse i string i Perl 5 Haskellflip i string i i string i flip Rakulists reverse i string i Erlangstrrev i string i PHP i string i 1 Python string reverse i string i Scheme SRFI 13 reverse i string i Common Lisp i string i reverse Ruby D modifies string new StringBuilder i string i reverse toString Javastd reverse i string i begin i string i end C a href Std string html class mw redirect title Std string std string a only modifies string StrReverse i string i VB i string i Reverse VB NET C implode rev explode i string i Standard ML i string i split reverse join JavaScriptstring reverse i string i i string i reverse Lua i string i reverse SmalltalkStringReverse i string i Mathematicareverse i string i PL I FUNCTION REVERSE i string i COBOL i string i toCharArray toList reversed join CobraString i string i characters reverse Swift 2 x String reverse i string i Swift 1 2 string reverse i string i Tcl i string i APL i string i chars rev collect lt String gt Rust 40 echo i string i rev Unix Example in Smalltalk hello reversed returns olleh Example in Perl 5 reverse hello returns olleh Example in Raku hello flip returns olleh Example in Python hello 1 returns olleh Example in Scheme use modules srfi srfi 13 string reverse hello returns olleh rfind edit Definition rfind i string i i substring i returns integerDescription Returns the position of the start of the last occurrence of substring in string If the substring is not found most of these routines return an invalid index value 1 where indexes are 0 based 0 where they are 1 based or some value to be interpreted as Boolean FALSE Related instrFormat Languages If not foundInStrRev i startpos i i string i i substring i VB returns 0instrrev i startpos i i string i i substring i FreeBASIC returns 0rindex i string i i substring i i startpos i Perl 5 returns 1rindex i string i i substring i i startpos i i string i rindex i substring i i startpos i Raku returns Nilstrrpos i string i i substring i i startpos i PHP returns FALSE i string i rfind i substring i i startpos i C STL returns std string nposstd string rfind i string i i substring i D returns 1 i string i rfind i substring i i startpos i i endpos i Python returns 1 i string i rindex i substring i i startpos i i endpos i raises ValueErrorrpos i string i i substring i i startpos i Seed7 returns 0 i string i rindex i substring i i startpos i Ruby returns nilstrings LastIndex i string i i substring i Go returns 1 i string i lastIndexOf i substring i i startpos i Java JavaScript returns 1 i string i LastIndexOf i substring i i startpos i i charcount i VB NET C Windows PowerShell F returns 1 search i substring i i string i from end t Common Lisp returns NIL i string i rangeOfString i substring i options NSBackwardsSearch location Objective C NSString only returns NSNotFoundStr search backward Str regexp string i substring i i string i Str length i string i 1 OCaml raises Not foundstring match i string i i substring i i string i match i substring i Lua returns nilAda Strings Unbounded Index Source gt i string i Pattern gt i substring i Going gt Ada Strings Backward Ada returns 0 i string i lastIndexOf i substring i i startpos i i charcount i Cobra returns 1 i string i lastIndexOfString i substring i Smalltalk returns 0string last i substring string startpos i Tcl returns 1 lt i substring i string 1 APL returns 1 i string i rfind i substring i Rust 41 returns None Examples in Common Lisp search e Hello mate from end t returns 9 search z word from end t returns NIL Examples in C Hello mate LastIndexOf e returns 9 Hello mate LastIndexOf e 4 returns 1 word LastIndexOf z returns 1 Examples in Perl 5 rindex Hello mate e returns 9 rindex Hello mate e 4 returns 1 rindex word z returns 1 Examples in Raku Hello mate rindex e returns 9 Hello mate rindex e 4 returns 1 word rindex z returns Nil Examples in Visual Basic InStrRev Hello mate e returns 10 InStrRev 5 Hello mate e returns 2 InStrRev word z returns 0 right edit Definition right i string i i n i returns stringDescription Returns the right n part of a string If n is greater than the length of the string then most implementations return the whole string exceptions exist see code examples Format Languagesstring string Last i n i 1 string Last AdaRight i string i i n i VBRIGHT i string i i n i BASICright i string i i n i FreeBASIC Ingres Pick Basicstrcpy i string2 i i string i i n i n must not be greater than the length of string C i string i Substring i string i Length i n i C i string i len i string i i n i Go i string i substring i string i length i n i Java i string i slice i n i JavaScript 42 right i string i i n i i padchar i Rexx Erlangsubstr i string i i n i Perl 5 PHPsubstr i string i i n i i string i substr i n i Raku i string i i n i Cobra Python i string param i i n i note the space after the colon Bash i string i i n i Pick Basic string take right i string i i n i Scheme SRFI 13 i string i i n i 1 Ruby i string i i n i D 43 String sub i string i String length i string i i n i i n i OCaml 26 string sub i string i i n i i string i sub i n i Lua i string i last i n i Smalltalk Squeak Pharo StringTake i string i i n i Mathematica 27 i string i 1 i n i COBOL i n i i string i APL i string i n i string i get n Rust 28 Examples in Java extract rightmost 4 characters String str CarDoor str substring str length 4 returns Door Examples in Raku abcde substr 3 returns cde abcde substr 8 out of range error Examples in Rexx right abcde 3 returns cde right abcde 8 returns abcde right abcde 8 returns abcde Examples in Scheme use modules srfi srfi 13 string take right abcde 3 returns cde string take right abcde 8 error Examples in Visual Basic Right sandroguidi 3 returns idi Right sandroguidi 100 returns sandroguidi rpartition edit Definition lt string gt rpartition separator Searches for the separator from right to left within the string then returns the sub string before the separator the separator then the sub string after the separator Description Splits the given string by the right most separator and returns the three substrings that together make the original Format Languages i string i rpartition i separator i Python Ruby 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 slice edit see substring split edit Definition lt string gt split separator limit splits a string on separator optionally only up to a limited number of substringsDescription Splits the given string by occurrences of the separator itself a string and returns a list or array of the substrings If limit is given after limit 1 separators have been read the rest of the string is made into the last substring regardless of whether it has any separators in it The Scheme and Erlang implementations are similar but differ in several ways JavaScript differs also in that it cuts it does not put the rest of the string into the last element See the example here The Cobra implementation will default to whitespace Opposite of join Format Languagessplit i separator i i string i i limit i Perl 5split i separator i i string i i limit i i string i split i separator i i limit i Rakuexplode i separator i i string i i limit i PHP i string i split i separator i i limit i 1 Python i string i split i separator i i limit i JavaScript Java Rubystring tokens i string i i sepchars i Erlangstrings Split i string i i separator i strings SplitN i string i i separator i i limit i Go string tokenize i string i i charset i i start i i end i Scheme SRFI 13 Split i string i i sepchars i i limit i VB i string i Split i sepchars i i limit i i options i VB NET C F i string i split i separator i i limit i i options i Windows PowerShellStr split Str regexp string i separator i i string i OCamlstd string split i string i i separator i D i string i componentsSeparatedByString i separator i Objective C NSString only i string i componentsSeparatedByString i separator i Swift Foundation TStringList Delimiter TStringList DelimitedText Object PascalStringSplit i string i i separator i i limit i Mathematica i string i split i sepchars i i limit i i options i Cobrasplit i string separator i Tcl i separator i i string i i string i in APL2 i separator i i string i in Dyalog APL 16 0 APL i string i split i separator i i string i split i limit i i separator i Rust 44 Example in C abc defgh ijk Split abc defgh ijk abc defgh ijk Split abc defgh ijk Example in Erlang string tokens abc defgh ijk abc defgh ijk Examples in Java abc defgh ijk split abc defgh ijk abc defgh ijk split abc defgh ijk Example in Pascal var lStrings TStringList lStr string begin lStrings TStringList Create lStrings Delimiter lStrings DelimitedText abc defgh ijk lStr lStrings Strings 0 abc lStr lStrings Strings 1 defgh lStr lStrings Strings 2 ijk end Examples in Perl 5 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 Examples in Raku Spam eggs spam spam and ham split spam Spam eggs and ham split X Spam eggs spam spam and ham Spam eggs spam spam and ham sprintf edit see Format strip edit see trim strcmp edit see Compare integer result substring edit See CharAt for base of startpos endpos Definition substring i string i i startpos i i endpos i returns stringsubstr i string i i startpos i i numChars i returns stringDescription Returns a substring of string between starting at startpos and endpos or starting at startpos of length numChars The resulting string is truncated if there are fewer than numChars characters beyond the starting point endpos represents the index after the last character in the substring Note that for variable length encodings such as UTF 8 UTF 16 or Shift JIS it can be necessary to remove string positions at the end in order to avoid invalid strings Format Languagesstring i startpos i i endpos i ALGOL 68 changes base index string i startpos i i endpos i Ada changes base index Mid i string i i startpos i i numChars i VBmid i string i i startpos i i numChars i FreeBASIC i string i i startpos i i numChars i IO APLMID i string i i startpos i i numChars i BASICsubstr i string i i startpos i i numChars i AWK changes string Perl 5 45 46 PHP 45 46 substr i string i i startpos i i numChars i i string i substr i startpos i i numChars i Raku 47 48 substr i string i i startpos i i numChars i i padChar i Rexx i string i i startpos i i endpos i Cobra Python 45 49 Go i string i i startpos i i numChars i Pick Basic i string i i startpos i i numChars i i string i i startpos i i endpos i 1 i string i i startpos i i endpos i Ruby 45 49 i string i i startpos i i endpos i i string i i startpos i len i numChars i Seed7 i string i slice i startpos i i endpos i JavaScript 45 49 i string i substr i startpos i i numChars i C STL JavaScript i string i Substring i startpos i i numChars i VB NET C Windows PowerShell F i string i substring i startpos i i endpos i Java JavaScriptcopy i string i i startpos i i numChars i Object Pascal Delphi substring i string i i startpos i i endpos i Scheme subseq i string i i startpos i i endpos i Common Lisp subseq i string i i startpos i i endpos i ISLISPString sub i string i i startpos i i numChars i OCamlsubstring i string i i startpos i i numChars i Standard MLstring sub string i string i i startpos i i endpos i string substr i string i i startpos i i numChars i Erlangstrncpy i result i i string i i startpos i i numChars i C i string i i startpos i i endpos i 1 Dtake i numChars i drop i startpos i i string i Haskell i string i substringWithRange NSMakeRange i startpos i i numChars i Objective C NSString only i string i i startpos i i endpos i F string sub i string i i startpos i i endpos i i string i sub i startpos i i endpos i Lua 45 49 i string i copyFrom i startpos i to i endpos i Smalltalk i string i i startpos i i endpos i FortranSUBSTRING i string i FROM i startpos i FOR i numChars i SQLStringTake i string i i startpos i i endpos i Mathematica 45 49 i string i i startpos i i numChars i COBOL i string param i i startpos i i numChars i Bashstring range string startpos endpos Tcl i string i i startpos i i endpos i i string i get i startpos i i endpos i Rust 28 Examples in C abc Substring 1 1 returns b abc Substring 1 2 returns bc abc Substring 1 6 error Examples in Common Lisp subseq abc 1 2 returns b subseq abc 2 returns c Examples in Erlang string substr abc 2 1 returns b string substr abc 2 returns bc Examples in Perl 5 substr abc 1 1 returns b substr abc 1 returns bc Examples in Raku abc substr 1 1 returns b abc substr 1 returns bc Examples in Python abc 1 2 returns b abc 1 3 returns bc Examples in Rexx substr abc 2 1 returns b substr abc 2 returns bc substr abc 2 6 returns bc substr abc 2 6 returns bc Uppercase edit Definition uppercase i string i returns stringDescription Returns the string in upper case Format LanguagesUCase i string i VBucase i string i FreeBASICtoupper i string i AWK changes string uc i string i Perl Raku i string i uc Rakutoupper i char i C operates on one character for size t i 0 len strlen i string i i lt len i i string i i toupper i string i i for char c i string i c 0 c c toupper c C string char array std string toUpper i string i Dtransform i string i begin i string i end i result i begin toupper sup id cite ref cpptransform 33 1 class reference a href cite note cpptransform 33 33 a sup C 50 uppercase i string i Object Pascal Delphi upcase i char i Object Pascal Delphi operates on one character strtoupper i string i PHPupper i string i Seed7 i string param i mnemonic is pointing up Bash a href Echo command html title Echo command echo a string a href Tr program html class mw redirect title Tr program tr a a z A Z Unixtranslate i string i UPPER variablesPARSE UPPER VAR SrcVar DstVar Rexx i string i upper Pythonupcase i string i Pick Basic i string i upcase Ruby 35 strings ToUpper i string i Go string upcase i string i Scheme Common LispString uppercase i string i OCamlString map Char toUpper i string i Standard MLmap Char toUpper i string i Haskell i string i toUpperCase Java JavaScriptto upper i string i Erlang i string i ToUpper VB NET C Windows PowerShell F i string i uppercaseString Objective C NSString only Swift Foundation string upper i string i i string i upper Lua i string i asUppercase SmalltalkUPPER i string i SQLToUpperCase i string i Mathematica FUNCTION UPPER CASE i string i COBOL i string i toUpper Cobrastring toupper i string i Tcl i string i to uppercase Rust 51 Example in C Wiki means fast ToUpper WIKI MEANS FAST Example in Perl 5 uc Wiki means fast WIKI MEANS FAST Example in Raku uc Wiki means fast WIKI MEANS FAST Wiki means fast uc WIKI MEANS FAST Example in Rexx translate Wiki means fast WIKI MEANS FAST Example 2 A This is an example UPPER A THIS IS AN EXAMPLE Example 3 A upper using Translate Function Translate UPPER VAR A Z Z UPPER USING TRANSLATE FUNCTION Example in Scheme use modules srfi srfi 13 string upcase Wiki means fast WIKI MEANS FAST Example in Visual Basic UCase Wiki means fast WIKI MEANS FAST trim edit Main article Trim programming trim or strip is used to remove whitespace from the beginning end or both beginning and end of a string Example usage Languages i String i Trim i chars i C VB NET Windows PowerShell i string i strip D trim i string i Clojure i sequence i predicate trim Factor string trim Space Tab Newline i string i Common Lisp string trim i string i Scheme i string i trim Java JavaScript 1 8 1 Firefox 3 5 Rust 52 Trim i String i Pascal 53 QBasic Visual Basic Delphi i string i strip Pythonstrings Trim i string i i chars i GoLTRIM RTRIM i String i Oracle SQL T SQLstrip i string i i option i i char i REXXstring strip i string i i option i i char i Erlang i string i strip i string i lstrip i string i rstrip Ruby i string i trim Rakutrim i string i PHP Raku i string i stringByTrimmingCharactersInSet NSCharacterSet whitespaceAndNewlineCharacterSet Objective C using Cocoa i string i withBlanksTrimmed i string i withoutSpaces i string i withoutSeparators Smalltalk Squeak Pharo Smalltalkstrip string SASstring trim i string i TclTRIM i string i TRIM ADJUSTL i string i FortranTRIM i string i SQLTRIM i string i LTrim i string i RTrim i String i ColdFusionString trim i string i OCaml 4 Other languagesIn languages without a built in trim function it is usually simple to create a custom function which accomplishes the same task APL edit APL can use regular expressions directly Trim R Alternatively a functional approach combining Boolean masks that filter away leading and trailing spaces Trim Or reverse and remove leading spaces twice Trim 2 AWK edit In AWK one can use regular expressions to trim ltrim v gsub t v rtrim v gsub t v trim v ltrim v rtrim v or function ltrim s sub t s return s function rtrim s sub t s return s function trim s return rtrim ltrim s C C edit There is no standard trim function in C or C Most of the available string libraries 54 for C contain code which implements trimming or functions that significantly ease an efficient implementation The function has also often been called EatWhitespace in some non standard C libraries In C programmers often combine a ltrim and rtrim to implement trim include lt string h gt include lt ctype h gt void rtrim char str char s s str strlen str while s gt str if isspace s break s 0 void ltrim char str size t n n 0 while str n 0 amp amp isspace unsigned char str n n memmove str str n strlen str n 1 void trim char str rtrim str ltrim str The open source C library Boost has several trim variants including a standard one 55 include lt boost algorithm string trim hpp gt trimmed boost algorithm trim copy string With boost s function named simply trim the input sequence is modified in place and returns no result Another open source C library Qt has several trim variants including a standard one 56 include lt QString gt 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 57 Haskell edit A trim algorithm in Haskell import Data Char isSpace trim String gt 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 edit The trim algorithm in J is a functional description trim amp That is filter for non space characters amp between leading and trailing spaces JavaScript edit 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 edit 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 edit 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 of 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 edit XSLT includes the function normalize space i string i which strips leading and trailing whitespace in addition to replacing any whitespace sequence including line breaks with a single space Example lt xsl variable name trimmed gt lt xsl value of select normalize space string gt lt xsl variable gt XSLT 2 0 includes regular expressions providing another mechanism to perform string trimming Another XSLT technique for trimming is to utilize the XPath 2 0 substring function References edit a b c d e the index can be negative which then indicates the number of places before the end of the string In Rust the str chars method iterates over code points and the std iter Iterator nth method on iterators returns the zero indexed nth value from the iterator or None the index can not be negative use N where N indicate the number of places before the end of the string In C the overloaded operator lt gt method on a string returns a std strong ordering object otherwise std weak ordering less equal same as equivalent or greater returns LESS EQUAL or GREATER returns LT EQ or GT returns TRUE or FALSE These functions are based on the ASCII collating sequence a b IBM extension In Rust the Ord cmp method on a string returns an Ordering Less Equal or Greater a b c d e f In Rust the operators and and the methods eq ne are implemented by the PartialEq trait and the operators lt gt lt gt and the methods lt gt le ge are implemented by the PartialOrd trait The operators use the compiler s default collating sequence modifies string sub 1 sub which must have enough space to store the result In Rust the operator is implemented by the Add trait See the str contains method See the std basic string contains method a b startpos is IBM extension a b See the str find method startpos is IBM extension scan in Fortran Wiki Fortranwiki org 2009 04 30 Retrieved 2013 08 18 verify in Fortran Wiki Fortranwiki org 2012 05 03 Retrieved 2013 08 18 formatstring must be a fixed literal at compile time for it to have the correct type See std format which is imported by the Rust prelude so that it can be used under the name format See the slice join method if n is larger than the length of the string then in Debug mode ArrayRangeException is thrown in Release mode the behaviour is unspecified if n is larger than the length of the string Java will throw an IndexOutOfBoundsException a b if n is larger than length of string raises Invalid argument a b if n is larger than length of string throw the message StringTake take a b c In Rust strings are indexed in terms of byte offsets and there is a runtime panic if the index is out of bounds or if it would result in invalid UTF 8 A amp str string reference can be indexed by various types of ranges including Range 0 n RangeFrom n and RangeTo n because they all implement the SliceIndex trait with str being the type being indexed The str get method is the non panicking way to index It returns None in the cases in which indexing would panic Ruby lacks Unicode support See the str len method In Rust the str chars method iterates over code points and the std iter Iterator count method on iterators consumes the iterator and returns the total number of elements in the iterator operates on one character a b The transform function exists in the std namespace You must include the lt algorithm gt header file to use it The tolower and toupper functions are in the global namespace obtained by the lt ctype h gt header file The std tolower and std toupper names are overloaded and cannot be passed to std transform without a cast to resolve a function overloading ambiguity e g std transform i string i begin i string i end i result i begin int int std tolower a href Std string html class mw redirect title Std string std string a only result is stored in string result which is at least as long as string and may or may not be string itself a b only ASCII characters as Ruby lacks Unicode support See the str to lowercase method See the str replace method a b c d e The find string in this construct is interpreted as a regular expression Certain characters have special meaning in regular expressions If you want to find a string literally you need to quote the special characters third parameter is non standard In Rust the str chars method iterates over code points the std iter Iterator rev method on reversible iterators std iter DoubleEndedIterator creates a reversed iterator and the std iter Iterator collect method consumes the iterator and creates a collection which here is specified as a String with the turbofish syntax from the iterator s elements See the str rfind method Annotated ES5 Es5 github com Archived from the original on 2013 01 28 Retrieved 2013 08 18 if n is larger than length of string then in Debug mode ArrayRangeException is thrown and unspecified behaviour in Release mode See the str split and str rsplit methods a b c d e f g startpos can be negative which indicates to start that number of places before the end of the string a b numChars can be negative which indicates to end that number of places before the end of the string startpos can not be negative use startpos to indicate to start that number of places before the end of the string numChars can not be negative use numChars to indicate to end that number of places before the end of the string a b c d e endpos can be negative which indicates to end that number of places before the end of the string a href Std string html class mw redirect title Std string std string a only result is stored in string result which is at least as long as string and may or may not be string itself In Rust the str to uppercase method returns a newly allocated String with any lowercase characters changed to uppercase ones following the Unicode rules In Rust the str trim method returns a reference to the original amp str Trim GNU Pascal prirucnik Gnu pascal de Retrieved 2013 08 24 String library comparison And org Retrieved 2013 08 24 Usage 1 54 0 Boost org 2013 05 22 Retrieved 2013 08 24 1 Archived August 2 2009 at the Wayback Machine dankamongmen sprezzos kernel packaging changelog at master dankamongmen sprezzos kernel packaging GitHub Github com Retrieved 2016 05 29 Retrieved from https en wikipedia org w index php title Comparison of programming languages string functions amp oldid 1211613105, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.