String functions (programming)

From Wikipedia, the free encyclopedia

This article is part of
the Programming Language Comparison
series.
General Comparison
Basic Syntax
String Operations
String Functions
List of "hello world" programs

Compatibility of C and C++
Comparison of C# and Java
Comparison of C# and Visual Basic .NET
Comparison of Pascal and C
Comparison of SQL variants

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.

Contents

[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] index

see #instr

[edit] 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([startpos,]string,substring) VB (startpos optional)
index(string,substring) Awk
index(string,substring[,startpos]) Perl
locate(string, substring) Ingres (if not found returns string length + 1)
strstr(string, substring) C
string.find(substring) C++
pos(substring, string) Delphi
string.index(substring) Python, Ruby
 ' 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] 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, 0, n) Awk (changes string), Perl
string[:n] Python
string[0..n] Ruby
string.substr(0,n) C++
leftstr(string, n) Delphi
 ' Example in VB
 left("sandroguidi", 3) would return the string "san". 
 left("sandroguidi", 100) would return the string "sandroguidi". 

[edit] len

see #length


[edit] 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.
Format Languages
length(string) Perl, Ingres, Delphi
len(string) VB, Python
string.size or
string.length
Ruby
strlen(string) C
string.length() C++
 # Examples in Perl 
 length("hello") returns 5.
 length("") returns 0.

[edit] locate

see #instr


[edit] mid

see #substring


[edit] replace

Other names sed
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
string.replace(find, replace) Python
string.gsub(find, replace) Ruby
echo "string" | sed 's/find/replace/g' Unix
 ' Examples in VB
 replace("effffff","f","jump") returns ejumpjumpjumpjumpjumpjump
 replace("blah", "z","y") returns blah.

[edit] slice

see #substring


[edit] strip

see #trim


[edit] strcmp

Other names compare
Definition strcmp(a:string,b:string) returns the equivalent of a-b.
Description Compares two strings to each other, if they equals textually, returns zero. If a is "textually bigger" returns a positive number and returns a negative number otherwise.

[edit] 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 (changes string), Perl
string[startpos:endpos] Python (endpos = startpos + offset)
string[startpos, offset] Ruby
string.slice(startpos, endpos) JavaScript
string.substr(startpos, length) C++
copy(string, startpos, length) Delphi
 Examples in Awk 
 substr("abc", 2,1) returns "b".
 substr("abc", 2,100) returns "bc". 

[edit] uppercase

Other names UCASE, toupper, upper, upcase
Definition uppercase(string) returns string
Description Returns the string in upper case.
Format Languages
UCASE(string) VB
toupper(string) Awk (changes string), Perl
toupper(char) C
transform (string.begin(), string.end(), string.begin(), toupper) C++
uppercase(string) Delphi
echo "string" | tr 'a-z' 'A-Z' Unix
string.upper() Python
string.upcase Ruby
 Example in VB
 Ucase("Wiki means fast?")
 would print "WIKI MEANS FAST?". 

[edit] trim

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] External links