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 functions 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)
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. String concatenation and regular expressions are handled in separate pages. Statements in guillemets (« … ») are optional.
CharAt
Definition charAt(string,integer) returns character. Description Returns character at index in the string. Equivalent See substring of length 1 character.
Format Languages Baseindex string[i]ALGOL 68, APL, Julia, Pascal, Object Pascal (Delphi), Seed71 string[i]C, C++, C#, Cobra, D, FreeBASIC, Go, Python, PHP, Ruby, Windows PowerShell, JavaScript, APL0 string{i}PHP (deprecated in 5.3)0 string(i)Ada≥1 Mid(string,i,1)VB1 MID$(string,i,1)BASIC1 string.Chars(i)VB.NET0 string(i:i)Fortran1 string.charAt(i)Java, JavaScript0 string.[i]OCaml, F#0 string.chars().nth(i)RustIn 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.0 string[i,1]Pick Basic1 String.sub (string, i)Standard ML0 string !! iHaskell0 (string-ref string i)Scheme0 (char string i)Common Lisp0 (elt string i)ISLISP0 (get string i)Clojure0 substr(string, i, 1)Perl 50 substr(string, i, 1)string.substr(i, 1)Raku0 substr(string, i, 1)PL/I1 substr(string, i, 1)REXX1 string.at(i)C++ (STL) (w/ bounds checking)0 lists:nth(i, string)Erlang1 [string characterAtIndex:i]Objective-C (NSString * only)0 string.sub(string, i, i)(string):sub(i, i)Lua1 string at: iSmalltalk (w/ bounds checking)1 string index string iTcl0 StringTake[string, {i}]Mathematica, Wolfram Language1 string@iEiffel1 string (i:1) COBOL1 ${string_param:i:1} Bash0 i⌷string APL0 or 1
{ Example in Pascal } var MyStr: string = 'Hello, World'; MyChar: Char; begin MyChar := MyStr; // 'e'
# Example in ALGOL 68 # "Hello, World"; // 'e'
// Example in C #include <stdio.h>
char myStr1[] = "Hello, World"; printf("%c", *(myStr1 + 1)); // 'e' printf("%c", *(myStr1 + 7)); // 'W' printf("%c", myStr1); // 'd' printf("%s", myStr1); // 'Hello, World' printf("%s", "Hello(2), World(2)"); // 'Hello(2), World(2)'
import std;
using std::string;
char myStr1[] = "Hello(1), World(1)"; string myStr2 = "Hello(2), World(2)"; std::println("Hello(3), World(3)"); // 'Hello(3), World(3)' std::println("{}", myStr2); // '2' std::println("{}", myStr1.substr(5, 3)); // '(1)'
// Example in C# "Hello, World"; // 'l'
# Example in Perl 5 substr("Hello, World", 1, 1); # 'e'
# Examples in Python "Hello, World" # '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)
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) FIALGOL 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 string2Perl, Raku string1 compare: string2Smalltalk (Squeak, Pharo) string1 <=> string2Ruby, C++ (STL, C++20)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. 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 string2OCaml String.compare (string1, string2)Standard MLreturns LESS, EQUAL, or GREATER compare string1 string2Haskellreturns LT, EQ, or GT [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)Fortranreturns .TRUE. or .FALSE.. These functions are based on the ASCII collating sequence. string1.localeCompare(string2)JavaScript string1string2))Go string compare string1 string2Tcl compare(string1,string2,count)PL/IIBM extension. string1.cmp(string2) RustIn Rust, the Ord::cmp method on a string returns an Ordering: Less, Equal, or Greater.
# 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) 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: and ALGOL 68 (stringOP? string1 string2), where OP can be any of and (operators starting with '-ci' are case-insensitive)Scheme (stringOP string1 string2), where OP can be any of and (operators starting with '' are case-insensitive)Scheme (SRFI 13) (stringOP string1 string2), where OP can be any of and (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 The original REXX used ¬ for Logical Not, ANSI Rexx uses \, some implementations accept ~ or ^, and non-EBCDIC implementations vary as to whether ¬ is at code point AA or AC.Rexx string1 OP string2, where OP can be any of and The original PL/I used ¬ for Logical Not, some implementations expect ^, and non-EBCDIC implementations vary as to whether ¬ is at code point AA or AC.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 and Perl, Raku string1 OP string2, where OP can be any of and C++ (STL), C#, D, Go, JavaScript, Python, PHP, Ruby, Rust,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. Swift string1 OP string2, where OP can be any of and (operators starting with '' 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 and FortranThe operators use the compiler's default collating sequence. 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
% 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
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 & string2Ada, FreeBASIC, Seed7, BASIC, VB, VB .NET, COBOL (between literals only) strcat(string1, string2) C, C++ (char * only)modifies string1, which must have enough space to store the result string1 . string2Perl, PHP string1 + string2ALGOL 68, C++ (STL), C#, Cobra, FreeBASIC, Go, Pascal, Object Pascal (Delphi), Java, JavaScript, Windows PowerShell, Python, Ruby, Rust,In Rust, the + operator is implemented by the Add trait. F#, Swift, Turing, VB string1 ~ string2D, Raku (string-append string1 string2)Scheme, ISLISP (concatenate 'string string1 string2)Common Lisp (str string1 string2)Clojure string1 || string2Rexx, SQL, PL/I string1 // string2Fortran string1 ++ string2Erlang, Haskell string1 ^ string2OCaml, Standard ML, F# [string1 stringByAppendingString:string2]Objective-C (NSString * only) string1 .. string2Lua string1 , string2Smalltalk, APL string1 string2SNOBOL string1string2Bash string1 <> string2Mathematicaconcat string1 string2Tcl
{ 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 PL/I */