Comparison of programming languages (string functions)
Adapted from Wikipedia · Discoverer experience
String functions are special tools used in computer programming languages to work with words and sentences, called strings. These tools help change or find information about strings in many ways.
Most programming languages that can handle words and sentences will have some string functions, even if there are other ways to work with them. In languages where everything is built from objects, string functions are often parts of string objects themselves. In some other languages, strings are shown as lists of tiny pieces, so tools that work with lists can also work with strings.
A very basic example of a string function is the length(string) function. This tells you how many letters are in a word or sentence. For example, length("hello world") would return 11, because "hello world" has 11 letters and spaces.
Many languages have similar string functions, which can help make coding easier and less confusing.
Common string functions (multi language reference)
String functions are common tools in many programming languages. They help manipulate or get information about text. These functions have different names across languages but do similar tasks.
CharAt
The CharAt function gets a single character from a specific position in a string. For example, in Pascal, to get the second character of "Hello, World", you would use MyStr, which returns 'e'.
Compare (integer result)
Some languages compare strings and return an integer. For example, in Perl, "hello" cmp "world" returns -1, showing that "hello" comes before "world" alphabetically.
Compare (relational operator-based, Boolean result)
Other languages use operators to compare strings and return a true or false result. In Erlang, "hello" > "world" returns false because "hello" is not greater than "world".
Concatenate
Concatenation joins two or more strings together. In C#, "abc" + "def" returns "abcdef".
Contains
The Contains function checks if a string includes another string. In C#, "Hello mate".Contains("e") returns true because "e" is in "Hello mate".
Equality
Equality checks if two strings are exactly the same. In C#, "hello" == "world" returns false.
Find
The Find function locates the position of a substring within a string. In Common Lisp, (search "e" "Hello mate") returns 1, showing that "e" starts at position 1 in "Hello mate".
Format
Format functions arrange strings with placeholders for values. In C#, String.Format("My {0} costs {1:C2}", "pen", 19.99) returns "My pen costs $19.99".
Inequality
Inequality checks if two strings are not the same. In C#, "hello" != "world" returns true.
Join
Join combines a list of strings into one string, often with a separator. In C#, String.Join("-", {"a", "b", "c"}) returns "a-b-c".
Length
Length returns the number of characters in a string. In C#, "hello".Length returns 5.
Lowercase
Lowercase changes all characters in a string to lowercase. In C#, "Wiki means fast?".ToLower() returns "wiki means fast?".
Replace
Replace swaps one substring for another in a string. In C#, "effffff".Replace("f", "jump") returns "ejumpjumpjumpjumpjumpjump".
Reverse
Reverse turns a string backwards. In Smalltalk, 'hello' reversed returns 'olleh'.
Right
Right gets a certain number of characters from the end of a string. In Visual Basic, Right("sandroguidi", 3) returns "idi".
Split
Split divides a string into parts based on a separator. In C#, "abc,defgh,ijk".Split(',') returns {"abc", "defgh", "ijk"}.
Substring
Substring takes a part of a string from a starting point. In C#, "abc".Substring(1, 1) returns "b".
Uppercase
Uppercase changes all characters in a string to uppercase. In C#, "Wiki means fast?".ToUpper() returns "WIKI MEANS FAST?".
Trim
Trim removes extra spaces from the start and end of a string. In JavaScript, a custom trim function can be added to remove spaces from both ends of a string.
| 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, PHP, Ruby, 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 | 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 | 0 |
| substr(string, i, 1) string.substr(i, 1) | Raku | 0 |
| substr(string, i, 1) | PL/I | 1 |
| substr(string, i, 1) | REXX | 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 |
| string at: i | Smalltalk (w/ bounds checking) | 1 |
| string index string i | Tcl | 0 |
| StringTake[string, {i}] | Mathematica, Wolfram Language | 1 |
| string@i | Eiffel | 1 |
| string (i:1) | COBOL | 1 |
| ${string_param:i:1} | Bash | 0 |
| i⌷string | APL | 0 or 1 |
| 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 string1string2 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) |
| string1.compare(string2) | C++ (STL), Swift (Foundation) |
| compare(string1, string2) | Rexx, Seed7 |
| compare(string1, string2, pad) | Rexx |
| 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 |
| compare string1 string2 | Haskell |
| [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 |
| string1.localeCompare(string2) | JavaScript |
| bytes.Compare([]byte(string1), []byte(string2)) | Go |
| string compare string1 string2 | Tcl |
| compare(string1,string2,count) | PL/I |
| string1.cmp(string2) | Rust |
| 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 =, <>, , = | Pascal, Object Pascal (Delphi), OCaml, Seed7, Standard ML, BASIC, VB, VB .NET, F# |
| string1 OP string2, where OP can be any of =, /=, ≠, , | ALGOL 68 |
| (stringOP? string1 string2), where OP can be any of =, -ci=, , -ci>, = and -ci>= (operators starting with '-ci' are case-insensitive) | Scheme |
| (stringOP string1 string2), where OP can be any of =, -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, , -greaterp, = and -not-lessp (the verbal operators are case-insensitive) | Common Lisp |
| (stringOP string1 string2), where OP can be any of =, /=, , = | ISLISP |
| string1 OP string2, where OP can be any of =, \=, , = | Rexx |
| string1 OP string2, where OP can be any of =, ¬=, , =, ¬ | PL/I |
| string1 OP string2, where OP can be any of =, /=, , = | Ada |
| string1 OP string2, where OP can be any of ==, /=, , == | Erlang |
| string1 OP string2, where OP can be any of ==, /=, , = | 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 ==, !=, , = | C++ (STL), C#, D, Go, JavaScript, Python, PHP, Ruby, Rust, 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 ==, ~=, , = | Lua |
| string1 OP string2, where OP can be any of =, ~=, , = | Smalltalk |
| string1 OP string2, where OP can be any of ==, /=, , =; Also: .EQ., .NE., .LT., .LE., .GT. and .GE. | Fortran |
| string1 OP string2 where OP can be any of =, <>, , = as well as worded equivalents | COBOL |
| string1 OP string2 where OP can be any of ==, <>, , = | 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 |
| Definition | concatenate(string1,string2) returns string. |
|---|---|
| Description | Concatenates (joins) two strings to each other, returning the combined string. 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 adjacent_to string2 | Rexx (abutment, equivalent to string1 || string2) |
| string1 whitespace string2 | Rexx (equivalent to string1 || ' ' || string2) |
| string1 & string2 | Ada, FreeBASIC, Seed7, BASIC, VB, VB .NET, COBOL (between literals only) |
| strcat(string1, string2) | C, C++ (char * only) |
| 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, 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 |
| 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, C++ (C++23) |
| 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 |
| 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) |
| ∨/substring⍷string | APL |
| Format | Languages |
|---|---|
| string1 == string2 | Python, C++ (STL), C#, Cobra, Go, JavaScript (similarity), PHP (similarity), Ruby, Rust, 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, Tcl |
| 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) |
| string1 ≡ string2 | APL |
| string1.eq(string2) | Rust |
| 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(«startpos,»string,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(«startpos,»string,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 | 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 |
| (substring⍷string)⍳1 | APL | returns 1 + the last position in string |
| string.find(substring) | Rust | returns None |
| 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 | 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 |
| string⍳char | APL | returns 1 + the last position in string |
| string.find(substring) | Rust | returns None |
| 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 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 |
| formatnumbers ⍕ items formatstring ⎕FMT items | APL | APL |
| format!(formatstring, items) | Rust | Python |
| Format | Languages |
|---|---|
| string1 ne string2 string1 NE string2 | ALGOL 68 – The operator "ne" occurs 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, 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 |
| string1 ≢ string2 | APL |
| string1.ne(string2) | Rust |
| 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 |
| (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↓∊separator,¨list_of_strings | APL |
| 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). For variable-length encodings such as UTF-8, UTF-16 or Shift-JIS, it can be necessary to remove string positions at the end, 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 |
| 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, 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 |
| 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 |
| string («FUNCTION» LENGTH(string) - n:n) | COBOL |
| string.substring(0, n) | Cobra |
| n↑string. | APL |
| string[0..n] string[..n] string.get(0..n) string.get(..n) | Rust |
| 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 | 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 |
| string.chars().count() | Number of Unicode code points | Rust |
| 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 |
| std.string.toLower(string) | D |
| transform(string.begin(), string.end(), result.begin(), ::tolower) | C++ |
| 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 |
| 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 |
| ToLowerCase[string] | Mathematica |
| «FUNCTION» LOWER-CASE(string) | COBOL |
| string.toLower | Cobra |
| string tolower string | Tcl |
| string.to_lowercase() | Rust |
| 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 |
| string.replaceAll(find_regex, replace) | Java |
| string.gsub(find, replace) | Ruby |
| string =~ s/find_regex/replace/g | Perl 5 |
| string.subst(find, replace, :g) | Raku |
| string.replace(find, replace, "g") string.replace(/find_regex/g, replace) | JavaScript |
| echo "string" | sed 's/find_regex/replace/g' | Unix |
| ${string_param//find_pattern/replace} | Bash |
| string.replace(find, replace) string -replace find_regex, replace | 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_regex ⊢ string | APL |
| 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::() | Rust |
| echo string | rev | Unix |
| 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(«startpos,» string,substring) | VB | returns 0 |
| instrrev(«startpos,» string,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 | returns None |
| 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 |
| 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} (a space occurs after the colon) | Bash |
| string[n] | Pick Basic |
| (string-take-right string n) | Scheme (SRFI 13) |
| string[-n..-1] | Ruby |
| string[$-n .. $] | D |
| String.sub string (String.length string - n) n | OCaml |
| string.sub(string, -n) (string):sub(-n) | Lua |
| string last: n | Smalltalk (Squeak, Pharo) |
| StringTake[string, -n] | Mathematica |
| string (1:n) | COBOL |
| ¯n↑string. | APL |
| string[n..] string.get(n..) | Rust |
| Definition | .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. |
| Definition | .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 |
| (separator≠string)⊂string in APL2 separator(≠⊆⊢)string in Dyalog APL 16.0 | APL |
| string.split(separator) string.split(limit, separator) | Rust |
| 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. For variable-length encodings such as UTF-8, UTF-16 or Shift-JIS, it can be necessary to remove string positions at the end, 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, PHP |
| substr(string, startpos, numChars) string.substr(startpos, numChars) | Raku |
| substr(string, startpos «,numChars, padChar») | PL/I |
| substr(string, startpos «,numChars, padChar») | Rexx |
| string[startpos:endpos] | Cobra, Python, Go |
| string[startpos, numChars] | Pick Basic |
| string[startpos, numChars] string[startpos .. endpos-1] string[startpos ... endpos] | Ruby |
| string[startpos .. endpos] string[startpos len numChars] | Seed7 |
| string.slice(startpos«, endpos») | JavaScript |
| 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 |
| string copyFrom: startpos to: endpos | Smalltalk |
| string(startpos:endpos) | Fortran |
| SUBSTRING(string FROM startpos «FOR numChars») | SQL |
| StringTake[string, {startpos, endpos}] | Mathematica |
| string (startpos:numChars) | COBOL |
| ${string_param:startpos:numChars} | Bash |
| string range string startpos endpos | Tcl |
| string[startpos..endpos] string.get(startpos..endpos) | Rust |
| 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); istring[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) | C++ |
| 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 |
| 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 |
| string.uppercase() | Kotlin |
| 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 |
| 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 |
| Trim(String) | Pascal, 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+ |
Related articles
This article is a child-friendly adaptation of the Wikipedia article on Comparison of programming languages (string functions), available under CC BY-SA 4.0.
Safekipedia