From Wikipedia, the free encyclopedia
String functions are used in computer programming languages to manipulate a string or query information about a string (some do both).
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.
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.
[edit] 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.
[edit] Compare
Definition |
compare(string1,string2) returns integer. |
Description |
Compares two strings to each other. If they are equivalent, returns zero. If string1 is lexicographically greater than string, returns a positive number; otherwise, returns a negative number. |
Format |
Languages |
cmp(string1, string2) |
Python |
strcmp(string1, string2) |
C, PHP |
string1 cmp string2 |
Perl |
string1 <=> string2 |
Ruby |
string1.compare(string2) |
C++ |
string1.compareTo(string2) |
Java |
# Example in Python
cmp("hello", "world") # returns -1
[edit] Concatenation
see concatenation
[edit] Equality
Tests if two strings are equal.
Format |
Languages |
string1 == string2 |
Python, C++, PHP, Ruby |
strcmp(string1, string2) == 0 |
C |
string1 = string2 |
VB, Delphi |
test string1 = string2, or
[ string1 = string2 ] |
Bourne Shell |
string1 eq string2 |
Perl |
string1.equals(string2) |
Java |
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 returns 0. |
Related |
instrrev |
Format |
Languages |
If not found |
instr([startpos,]string,substring) |
VB (startpos optional) (positions start at 1) |
returns 0 |
index(string,substring) |
Awk |
index(string,substring[,startpos]) |
Perl |
returns -1 |
strpos(string,substring[,startpos]) |
PHP |
returns FALSE |
locate(string, substring) |
Ingres |
returns string length + 1 |
strstr(string, substring) |
C (returns pointer to first character) |
returns NULL |
pos(substring, string) |
Delphi |
returns 0 |
string.find(substring[,startpos]) |
C++ |
returns string::npos |
string.find(substring[,startpos[,endpos]]) |
Python |
returns -1 |
string.index(substring[,startpos]) |
Ruby |
returns nil |
string.indexOf(substring[,startpos]) |
Java |
returns -1 |
' Example in VB
instr("Hello mate", "ell") ' would return 2
instr(5, "Hello mate", "e") ' would return 10
instr("word", "z") ' would return 0.
[edit] Inequality
Tests if two strings are not equal. See also #Equality.
Format |
Languages |
string1 <> string2 |
VB, Delphi |
string1 ne string2 |
Perl |
test string1 != string2, or
[ string1 != string2 ] |
Bourne Shell |
see #Find
see #Find
Definition |
join(separator, list_of_strings) joins a list of strings 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. |
Format |
Languages |
join(separator, list_of_strings) |
Perl |
implode(separator, array_of_strings) |
PHP |
separator.join(sequence_of_strings) |
Python |
array_of_strings.join(separator) |
Ruby |
# Examples in Python
mystr = "-".join("a", "b", "c") ### ('a-b-c')
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 the whole string is returned. |
Format |
Languages |
left(string,n) |
VB, Ingres |
substr(string, 0, n) |
Awk (changes string), Perl, PHP |
string[:n] |
Python |
string[0..n] |
Ruby |
string.substr(0,n) |
C++ |
leftstr(string, n) |
Delphi |
string.substring(0,n) |
Java |
' Example in VB
left("sandroguidi", 3) ' would return the string "san".
left("sandroguidi", 100) ' would return the string "sandroguidi".
see #length
[edit] length
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. |
# Examples in Perl
length("hello") ### returns 5.
length("") ### returns 0.
[edit] locate
see #instr
[edit] Lowercase
see also #Uppercase
see #substring
[edit] partition
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 |
string.partition(separator) |
Python, Ruby |
# 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', "", "")
[edit] replace
Definition |
replace(string, find, replace) returns string |
Description |
Returns a string the with find occurrences changed to replace. |
Format |
Languages |
replace(string, find, replace) |
VB |
str_replace(find, replace, string) |
PHP |
string.replace(find, replace) |
Python, Java |
string.gsub(find, replace) |
Ruby |
string =~ s/find/replace/g |
Perl |
string.replace(/find/g, replace) |
JavaScript (Regular Expressions can be used) |
echo "string" | sed 's/find/replace/g' |
Unix |
' Examples in VB
replace("effffff","f","jump") ' returns "ejumpjumpjumpjumpjumpjump"
replace("blah", "z","y") ' returns "blah".
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 the whole string is returned. |
' Example in VB
right("sandroguidi", 3) ' would return the string "idi".
right("sandroguidi", 100) ' would return the string "sandroguidi".
[edit] 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. |
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')
see #substring
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 int. Opposite of join. |
Format |
Languages |
split(/separator/, string[, limit]) |
Perl |
explode(separator, string[, limit]) |
PHP |
string.split(separator[, limit]) |
Javascript, Java, Python, Ruby |
# Examples in Python
"Spam eggs spam spam and ham".split('spam') ### ('Spam eggs ', ' ', ' and ham')
"Spam eggs spam spam and ham".split('X') ### ('Spam eggs spam spam and ham')
see #trim
[edit] strcmp
see #compare
[edit] substring
Definition |
substr(string, startpos, offset) returns string |
Description |
Returns a substring of string starting at startpos of length offset. If offset goes past the end of the string then only the string form startpos to end of string is returned. |
Format |
Languages |
mid(string, startpos, offset) |
VB |
substr(string, startpos, offset) |
Awk (changes string), Perl, PHP |
string[startpos:endpos] |
Python (endpos = startpos + offset) |
string[startpos, offset] |
Ruby |
string.slice(startpos, endpos) |
JavaScript |
string.substr(startpos, offset) |
C++ |
string.substring(startpos, endpos) |
Java |
copy(string, startpos, offset) |
Delphi |
char result[offset+1] = "";
strncat(result, string + startpos, offset);
|
C |
Examples in Awk
substr("abc", 2,1) # returns "b".
substr("abc", 2,100) # returns "bc".
[edit] uppercase
Definition |
uppercase(string) returns string |
Description |
Returns the string in upper case. |
Format |
Languages |
UCASE(string) |
VB |
toupper(string) |
Awk (changes string) |
uc(string) |
Perl |
toupper(char) |
C (operates on a single character) |
transform(string.begin(), string.end(), result.begin(), toupper) |
C++ (result is stored in string result which is at least as long as string, and may or may not be string itself) |
uppercase(string) |
Delphi |
strtoupper(string) |
PHP |
echo "string" | tr 'a-z' 'A-Z' |
Unix |
string.upper() |
Python |
string.upcase |
Ruby |
string.toUpperCase() |
Java |
' Example in VB
Ucase("Wiki means fast?") ' would return "WIKI MEANS FAST?".
- see Trim (programming).
trim or strip is used to remove whitespace from the beginning, end, or both beginning and end, of a string.
[edit] Concatenation
Joining two strings together is called concatenation. Some languages support this natively in the language (VB) and some do not (C).
[edit] Libraries
In C, it is often safer and faster to use pre-written libraries for a string ADT, instead of trying to use the fragile builtin functions. Some examples being Vstr[1] and the Better String Library[2].
[edit] External links