Most computer programming languages that have a string datatype will have some string functions although it should be noted that 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 both Prolog and Erlang, a string is represented as a list (of character codes), therefore all list-manipulation procedures are applicable, though the latter also implements a set of such procedures that are string-specific.
The most basic example of a string function is the length(string) function. This function returns the length of a string literal.
eg. 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)
Here is a list of common string functions which are found in other languages. Any other equivalent functions used by other languages are also listed. 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.
# Examples in Python"Hello, World"[2]# 'l'"Hello, World"[-3]# 'r'
' Example in Visual BasicGetChar("Hello, World",2)' "e"c
' Example in Visual Basic .NET"Hello, World".Chars(2)' "l"c
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.
# Example in Pythoncmp("hello","world")# returns -1
/** Example in REXX */
compare("hello", "world") /* returns index of mismatch: 1 */
; Example in Scheme(use-modules(srfisrfi-13)); returns index of mismatch: 0(string-compare"hello""world"valuesvaluesvalues)
Compare (integer result, fast/non-human ordering)
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 greater than, or less than, respectively, than string2. The reason for the difference between this and #Compare (integer result) is that no ordering guarantees are given, leaving the implementation to do whatever ordering is fastest, a common implementation is to order by length and then by byte value (which can be significantly faster, but much less useful to humans).
Format
Languages
if (!(ret = (string1_len - string2_len))) ret = memcmp(string1, string2, string1_len)
(stringX string1string2), where X can be any of =, -ci=, <>, -ci<>, <, -ci<, >, -ci>, <=, -ci<=, >= and -ci>= (operators starting with '-ci' are case-insensitive)
(stringX string1string2), where X can be any of =, -equal, /=, -not-equal, <, -lessp, >, -greaterp, <=, -not-greaterp, >= and -not-lessp (the verbal operators are case-insensitive)
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)
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.
' Example in Visual Basic"abc"&"def"' returns "abcdef"
// Example in D"abc"~"def";// returns "abcdef"
Equality
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 programer but is often a significantly more expensive operation, this is especially true when using "C-strings".
' Example in Visual Basic"hello"="world"' returns false
# Example in Windows PowerShell
"hello" -eq "world" # returns false
Find
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.
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 a single character. Also, in many languages, characters and strings are different types, so it is convenient to have such a function.
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).
D (if n is largerer than length of string, then in Debug mode ArrayRangeException is thrown, in Release mode behaviour is unspecified, mostly Segmentation Fault)
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.
; Example in Scheme(use-modules(srfisrfi-13))(string-reverse"hello"); returns "olleh"
rfind
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.
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).
; Examples in Scheme(use-modules(srfisrfi-13))(string-take-right"abcde",3); returns "cde" (string-take-right"abcde",8); error
' Examples in Visual BasicRight("sandroguidi",3)' returns "idi" Right("sandroguidi",100)' returns "sandroguidi"
// Examples in Java; extract rightmost 4 charactersStringstr="CarDoor";str.substring(str.length()-4,str.length());// returns 'Door'
rpartition
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.
<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. Opposite of join.
Returns a substring of string starting at startpos of length numChars. The resulting string is truncated if there are fewer than numChars characters beyond the starting point
trim or strip is used to remove whitespace from the beginning, end, or both beginning and end, of a string.
Notes
^ abcdeThe "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.