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.
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.
index
see instr
instr
| Other names
| index, locate
|
| Definition
| instr(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
|
| instr(*string,substring)
| VB - startpos optional.
|
| index(string,substring)
| Awk
|
| index(string,substring*)
| Perl
|
| locate(string, substring)
| Ingres - Note if not found returns string length + 1
|
| strstr(string, substring)
| C
|
| string.find(substring)
| C++
|
' Example in
VB
instr("Hello mate", 3) ' would return 2
instr(5, "Hello mate", 3) ' would return 10
instr("word", "z") ' would return 0.
left
| Other names
| left, substr
|
| 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, 1, n)
| Awk, Perl - note changes string.
|
| string*
| Python
|
| string.substr(0,n)
| C++
|
' Example in
VB
left("word", 3) would return the string "wor".
left("word", 100) would return the string "word".
len
see length
length
| Other names
| len, 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.
locate
see instr
mid
see substring
slice
see substring
strcmp
| Other names
| compare
|
| Definition
| strcmp(a:string,b:string) returns string a or false.
|
| Description
| Compares two strings to each other, if they equals textually, returns the string, if not, returns false.
|
substring
| Other names
| mid, substr, slice
|
| 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, Perl - note changes string.
|
| string*
| Python - note endpos = startpos + offset
|
| string.slice(startpos, endpos)
| JavaScript
|
| string.substr(startpos, length)
| C++
|
Examples in
Awk
substr("abc", 2,1) returns "b".
substr("abc", 2,100) returns "bc".
uppercase
| Other names
| UCASE, toupper
|
| Definition
| uppercase(string) returns string
|
| Description
| Returns the string in upper case.
|
| Format | Languages
|
| UCASE(string)
| VB.
|
| toupper(string)
| Awk - changes string. >Awk, Perl.
|
| toupper(char)
| C.
|
| transform (string.begin(), string.end(), string.begin(), toupper)
| C++.
|
| echo "string" > tr 'a-z' 'A-Z'
| Unix.
|
| string.upper()
| Python
|
'Example in
VB
? Ucase("Wiki means fast?") ' would print "WIKI MEANS FAST?".
trim
- see Trim (programming).
trim or
strip is used to remove surrounding whitespace from a string.
Concatenation
Joining two strings together is called
concatenation. Some languages support this natively in the language (
VB) and some do not (
C).
External links
Character encoding | Algorithms on strings