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
# Example in ALGOL 68 # "Hello, World"[2]; // 'e'
Compare (integer result)
Compare (relational operator-based, Boolean result)
Concatenation
Contains
¢ Example in ALGOL 68 ¢ string in string("e", loc int, "Hello mate"); ¢ returns true ¢ string in string("z", loc int, "word"); ¢ returns false ¢
Equality Tests if two strings are equal. See also #Compare and #Compare. 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".
Find
Examples
Common Lisp
C#
Raku
Scheme
Visual Basic
Smalltalk
Find character
^a Given a set of characters, SCAN returns the position of the first character found, while VERIFY returns the position of the first character that does not belong to the set.
Format
Inequality Tests if two strings are not equal. See also #Equality.
index see #Find
indexof see #Find
instr see #Find
instrrev see #rfind
join
lastindexof see #rfind
left
len see #length
length
locate see #Find
Lowercase
mid see #substring
partition
replace
reverse
rfind
right
rpartition
slice see #substring
split
sprintf see #Format
strip see #trim
strcmp see #Compare (integer result)
substring
Uppercase
trim
trim or strip is used to remove whitespace from the beginning, end, or both beginning and end, of a string.
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 APL can use regular expressions directly:
Alternatively, a functional approach combining Boolean masks that filter away leading and trailing spaces:
Or reverse and remove leading spaces, twice:
AWK In AWK, one can use regular expressions to trim:
or:
C/C++ There is no standard trim function in C. Most of the available string libraries 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:
C++'s std::string_view has a remove_prefix(n) method, which moves the start of the view forward by n characters.
C++ also achieves string trimming using std::views::drop_while, rather than by a trim() method:
Another open source C++ library Qt, has several trim variants, including a standard method for QString:
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.
Haskell A trim algorithm in Haskell:
may be interpreted as follows: f drops the preceding whitespace, and reverses the string. f is then again applied to its own output. The type signature (the second line) is optional.
J The trim algorithm in J is a functional description:
That is: filter (#~) for non-space characters (' '&~:) between leading (+./\) and (*.) trailing (+./\.) spaces.
JavaScript 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:
Perl Perl 5 has no built-in trim function. However, the functionality is commonly achieved using regular expressions. Example:
or:
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:
PL/I The PL/I trim BIF can trim characters from the left or the right. The syntax is trim(source[,left[,right]]). left specifies the characters to remove from the left while right specifies the characters to remove from the right; both default to a single space. Examples:
foo = trim(' Hi! ') /* 'Hi!' */; foo = trim('*Hi!*','*') /* 'Hi!*' */; foo = trim('*Hi!*','*','*') /* 'Hi!' */;
Rexx The Rexx strip BIF can remove leading and trailing characters. The syntax is where option may be Both (default), Leading or Trailing and char must be a single character (default ' '). Example:
… excerpt ends here. Continue reading the full article.