BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help
Not What You Meant?  There are 13 definitions for Chuck.

String functions (programming)

Print-Friendly
About 11 pages (3,169 words)

Bookmark and Share Questions on this topic? Just ask!
This article is part of
the Programming Language Comparison
series.
General Comparison
Basic Syntax
Basic Instructions
Arrays
Associative arrays
String Operations
String Functions

Evaluation strategy
List of "hello world" programs

Comparison of ALGOL 68 and C++
Compatibility of C and C++
Comparison of C and Pascal
Comparison of C++ and Java
Comparison of C# and Java
Comparison of C# and Visual Basic .NET
Comparison of ABAP and Java
This article presents string functions given in computer programming. For string functions in formal language theory, see string operations.

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. 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.

Contents

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.

CharAt

Definition charAt(string,integer) returns character.
Description Returns character at index in the string.
Equivalent See substring of length 1 character.
Format Languages
string[i] C, C++, C#, Python, PHP, Ruby
string.Chars(i) VB.NET
string.charAt(i) Java
string.[i] OCaml
(string-ref string i) Scheme
substr(string, i, 1) Perl
string.at(i) C++ (w/ bounds checking)

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.
Format Languages
cmp(string1, string2) Python
strcmp(string1, string2) C, PHP
string1 cmp string2 Perl
string1 <=> string2 Ruby
string1.compare(string2) C++
compare(string1, string2) REXX
CompareStr(string1, string2) Pascal, Object Pascal (Delphi)
string1.compareTo(string2) Java
string1.CompareTo(string2) VB .NET, C#
(string-compare string1 string2 p< p= p>) Scheme
compare string1 string2 OCaml
[string]::Compare('string1','string2') Windows PowerShell
 # Example in Python 
 cmp("hello", "world")      # returns: -1
 /** Example in REXX */ 
 compare("hello", "world")  /* returns index of mismatch: 1 */
 ; Example in Scheme 
 (use-modules (srfi srfi-13))
 ; returns index of mismatch: 0
 (string-compare "hello" "world" values values values)

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) C
ustr_cmp_fast(string1, string2) Ustr string library
(length(string1) <=> length(string2) ¦¦ string1 cmp string2) Perl
cmp(len(string1), len(string2)) or cmp(string1, string2) Python
(Length(string1) <> Length(string2)) or CompareStr(string1, string2) Pascal, Object Pascal (Delphi)

Compare (relational operator-based, Boolean result)

Definition string1 op string2 OR (compare string1 string2) returns Boolean.
Description Lexicographically compares two strings using a relational operator or function. Boolean result returned.
Format Languages
string1 op string2, where op can be any of =, <>, <, >, <= and >= Pascal, Object Pascal (Delphi), OCaml, VB .NET
(stringX? string1, string2), where X can be any of =, <, >, <= and >= Scheme
string1 op string2, where op can be any of =, \=, <, >, <= and >= REXX
string1 op string2, where op can be any of ==, /=, <, >, =< and >= Erlang
string1 op string2, where op can be any of eq, ne, lt, gt, le and ge Perl
string1 op string2, where op can be any of ==, !=, <, >, <= and >= C++
string1 op string2, where op can be any of -eq, -ne, -lt, -gt, -le and -ge Windows PowerShell
 % Example in Erlang 
 "hello" > "world".            % returns: false

Concatenation

Main article: Concatenation
Definition concatenate(string1,string2) returns string.
Description 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.
Format Languages
string1 & string2 VB, VB .NET
strcat(string1, string2) C
string1 . string2 Perl, PHP
string1 + string2 C++, Pascal, Object Pascal (Delphi), Java, Windows PowerShell
(string-append string1 string2) Scheme
string1 || string2 REXX,Fortran
string1 ++ string2 Erlang
string1 ^ string2 OCaml
string1 ++ string2 Haskell

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".

Format Languages
string1 == string2 Python, C++, PHP, Ruby, Erlang, Haskell
strcmp(string1, string2) == 0 C
(string=? string1 string2) Scheme
string1 = string2 VB, Pascal, Object Pascal (Delphi), REXX, OCaml
test string1 = string2, or
[ string1 = string2 ]
Bourne Shell
string1 eq string2 Perl
string1.equals(string2) Java
string1 -eq string2 Windows PowerShell

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.
Related instrrev
Format Languages If not found
InStr([startpos,]string,substring) VB (startpos optional) (positions start at 1) returns 0
index(string,substring) Awk returns 0
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) Pascal, Object Pascal (Delphi) returns 0
pos(substring, string[,startpos]) REXX 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, Windows PowerShell returns -1
string.IndexOf(substring[,startpos[, charcount]]) VB .NET, C# returns -1
string:str(string, substring)) Erlang returns 0
(string-contains string substring) Scheme returns #f
 ' Example in VB
 InStr("Hello mate", "ell")            ' returns: 2
 InStr(5, "Hello mate", "e")           ' returns: 10
 InStr("word", "z")                    ' returns: 0
 ; Example in Scheme
 (use-modules (srfi srfi-13))
 (string-contains "Hello mate", "ell") ; returns: 1
 (string-contains "word", "z")         ; returns: #f

Inequality

Tests if two strings are not equal. See also #Equality.

Format Languages
string1 <> string2 VB, Pascal, Object Pascal (Delphi), OCaml
string1 ne string2 Perl
(not (string=? string1 string2)) Scheme
string1 \= string2 REXX
string1 /= string2 Erlang, Haskell
test string1 != string2, or
[ string1 != string2 ]
Bourne Shell/Python
string1 -ne string2 Windows PowerShell

index

see #Find Joe #Check

instr

see #Find

join

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
(string-join array_of_strings separator) Scheme
String.concat separator list_of_strings OCaml
String.Join(separator, array_of_strings) VB .NET, C#
&{$OFS=$separator; "$array_of_strings"} Windows PowerShell
 # Example in Python
 mystr = "-".join(["a", "b", "c"])  # ('a-b-c')
 ; Example in Scheme
 (use-modules (srfi srfi-13))
 (string-join '("a" "b" "c") "-")   ; "a-b-c"

left

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 most implementations return the whole string (exceptions exist - see code examples).
Format Languages
Left(string,n) VB
left(string,n) Ingres
left(string,n [,padchar]) REXX, Erlang
substr(string, 0, n) Awk (changes string), Perl, PHP
string[:n] Python
string[0..n] Ruby
string.substr(0,n) C++
leftstr(string, n) Pascal, Object Pascal (Delphi)
string.substring(0,n) Java, Windows PowerShell
(string-take string n) Scheme
take n string Haskell
 ' Example in VB
 Left("sandroguidi", 3)   ' returns: "san" 
 Left("sandroguidi", 100) ' returns: "sandroguidi" 
 /* Example in REXX */
 left("abcde", 3)         /* returns: "abc"      */
 left("abcde", 8)         /* returns: "abcde   " */
 left("abcde", 8, "*")    /* returns: "abcde***" */
 ; Example in Scheme
 (use-modules (srfi srfi-13))
 (string-take "abcde", 3) ; returns: "abc" 
 (string-take "abcde", 8) ; returns: error

len

see #length

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, Pascal, Object Pascal (Delphi), REXX
len(string) Python, Erlang
Len(string) VB
string.Length VB .NET, C#
string.size OR string.length Ruby, Windows PowerShell
strlen(string) C, PHP
string.length() C++, Java
(string-length string) Scheme
String.length string OCaml
length string Haskell
 # Examples in Perl 
 length("hello")      # returns: 5
 length("")           # returns: 0
 # Examples in Erlang 
 string:len("hello"). % returns: 5
 string:len("").      % returns: 0

locate

see #Find

Lowercase

see also #Uppercase

mid

see #substring

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', "", "")

replace

Definition replace(string, find, replace) returns string
Description Returns a string the with find occurrences changed to replace.
Format Languages
changestr(find, string, replace) REXX
Replace(string, find, replace) VB
string.Replace(find, replace) VB .NET, C#
str_replace(find, replace, string) PHP
string.replace(find, replace) Python, Java
string.gsub(find, replace) Ruby
string =~ s/find/replace/g Perl
replace(string.begin(), string.end(), find, replace) C++
string.replace(/find/g, replace) JavaScript (Regular Expressions can be used)
echo "string" | sed 's/find/replace/g' Unix
string.replace(find, replace), or
string -replace find, replace
Windows PowerShell
(The -replace operator uses Regular Expressions)
 ' Examples in VB
 Replace("effffff","f","jump") ' returns "ejumpjumpjumpjumpjumpjump"
 Replace("blah", "z","y")      ' returns "blah".

right

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 most implementations return the whole string (exceptions exist - see code examples).
Format Languages
Right(string,n) VB
right(string,n) Ingres
right(string,n [,padchar]) REXX, Erlang
substr(string,-n) Perl, PHP
string[-n:] Python
(string-take-right string n) Scheme
 ' Example in VB
 Right("sandroguidi", 3)        ' returns: "idi" 
 Right("sandroguidi", 100)      ' returns: "sandroguidi" 
 /* Example in REXX */
 right("abcde", 3)              /* returns: "cde"      */
 right("abcde", 8)              /* returns: "   abcde" */
 right("abcde", 8, "*")         /* returns: "***abcde" */
 ; Example in Scheme
 (use-modules (srfi srfi-13))
 (string-take-right "abcde", 3) ; returns: "cde" 
 (string-take-right "abcde", 8) ; returns: error

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')

slice

see #substring

split

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 it. The Scheme and Erlang implementations are similar but differ in several ways. Opposite of join.
Format Languages
split(/separator/, string[, limit]) Perl
explode(separator, string[, limit]) PHP
string.split(separator[, limit]) Javascript, Java, Python, Ruby
tokens(string, sepchars) Erlang
string-split(string, sepchar) Scheme
string.Split(sepchars[, limit[, options]]) VB .NET, C#
string.split(separator) Windows PowerShell
 # Example 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')
 % Example in Erlang
 string:tokens("abc;defgh;ijk", ";").  % ["abc", "defgh", "ijk"]
 ; Example in Scheme
 (string-split "abc;defgh;ijk" #\;)    ; ("abc" "defgh" "ijk")

strip

see #trim

strcmp

see #Compare (integer result)

substring

Definition substr(string, startpos, numChars) returns string
Description 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
Format Languages
Mid(string, startpos, numChars) VB
substr(string, startpos, numChars) Awk (changes string), Perl, PHP
substr(string, startpos [,numChars, padChar]) REXX
string[startpos:endpos] Python (endpos = startpos + numChars)
string[startpos, numChars] Ruby
string.slice(startpos, endpos) JavaScript
string.substr(startpos, numChars) C++
string.Substring(startpos, numChars) VB .NET, C#
string.substring(startpos, endpos) Java
copy(string, startpos, numChars) Delphi
(string-copy string startpos endpos) Scheme
String.sub 'string startpos numChars Ocaml
substr(string, startpos, numChars) Erlang
char result[numChars+1] = "";

strncat(result, string + startpos, numChars);

C
string.substring(startpos, numChars) Windows PowerShell
 # Example in Awk 
 substr("abc", 2, 1)         # returns: "b"
 substr("abc", 2, 6)         # returns: "bc"
 /* Example in REXX */
 substr("abc", 2, 1)         /* returns: "b"      */
 substr("abc", 2)            /* returns: "bc"     */
 substr("abc", 2, 6)         /* returns: "bc    " */
 substr("abc", 2, 6, "*")    /* returns: "bc****" */
 % Example in Erlang
 string:substr("abc", 2, 1).  % returns: "b"
 string:substr("abc", 2).     % returns: "bc"

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
translate(string) REXX
string.upper() Python
string.upcase Ruby
(string-upcase string) Scheme
String.uppercase string OCaml
Data.Char.toUpper char Haskell (operates on a single character; 'Data.' is optional)
string.toUpperCase() Java
to_upper(string) Erlang
string.ToUpper() Windows PowerShell, VB .NET, C#
 ' Example in VB
 UCase("Wiki means fast?")          ' "WIKI MEANS FAST?" 
 /* REXX */
 translate("Wiki means fast?")      /* "WIKI MEANS FAST?" */
 ; Scheme
 (use-modules (srfi srfi-13))
 (string-upcase "Wiki means fast?") ; "WIKI MEANS FAST?"

trim

Main article: Trim (programming)

trim or strip is used to remove whitespace from the beginning, end, or both beginning and end, of a string.

External links

View More Summaries on String functions (programming)
 
Ask any question on String functions (programming) and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
String functions (programming) from Wíkipedia. ©2006 by Wíkipedia. Licensed under the GNU Free Documentation License. View a list of authors or edit this article.

Article Navigation
Join BookRagslearn moreJoin BookRags




About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy