Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

Comparison of programming languages

(string functions)
From Wikipedia, the free encyclopedia
Jump to: navigation, search
"String functions" redirects here. For string functions in formal language theory, see String
operations.
Programming language
comparisons

General comparison

Basic syntax

Basic instructions

Arrays

Associative arrays

String operations

String functions

List comprehension

Object-oriented programming

Object-oriented constructors

Database access

Database RDBMS
Evaluation strategy
List of "hello world" programs

ALGOL 58's influence on ALGOL 60


ALGOL 60: Comparisons with other
languages
Comparison of ALGOL 68 and C++
ALGOL 68: Comparisons with other
languages
Compatibility of C and C++
Comparison of Pascal and Borland
Delphi
Comparison of Pascal and C
Comparison of Java and C++
Comparison of Java and C#
Comparison of C# and Visual
Basic .NET
This box: view • talk • edit

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

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

 1 Common String Functions (multi language reference)


o 1.1 CharAt
o 1.2 Compare (integer result)
o 1.3 Compare (relational operator-based, Boolean result)
o 1.4 Concatenation
o 1.5 Contains
o 1.6 Equality
o 1.7 Find
o 1.8 Find character
o 1.9 Format
o 1.10 Inequality
o 1.11 index
o 1.12 indexof
o 1.13 instr
o 1.14 instrrev
o 1.15 join
o 1.16 lastindexof
o 1.17 left
o 1.18 len
o 1.19 length
o 1.20 locate
o 1.21 Lowercase
o 1.22 mid
o 1.23 partition
o 1.24 replace
o 1.25 reverse
o 1.26 rfind
o 1.27 right
o 1.28 rpartition
o 1.29 slice
o 1.30 split
o 1.31 sprintf
o 1.32 strip
o 1.33 strcmp
o 1.34 substring
o 1.35 Uppercase
o 1.36 trim
 2 Notes
 3 External links

[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. Statements in guillemets (« … ») are
optional.

[edit] CharAt

Definition charAt(string,integer) returns character.


Description Returns character at index in the string.
Equivalent See substring of length 1 character.
Base
Format Languages
index
string[i] ALGOL 68, Pascal, Object Pascal (Delphi) 1
string[i] C, C++, C#, D, Go, Python1, PHP, Ruby1, Windows 0
PowerShell
string{i} PHP (deprecated in 5.3) 0
string(i) Ada ≥1
Mid(string,i,1) VB 1
string.Chars(i) VB.NET 0
string(i:i) Fortran 1
string.charAt(i) Java, JavaScript 0
string.[i] OCaml, F# 0
String.sub (string, i) Standard ML 0
string !! i Haskell 0
(string-ref string i) Scheme 0
(char string i) Common Lisp 0
substr(string, i, 1) Perl1 0
string.at(i) C++ (std::string only) (w/ bounds checking) 0
lists:nth(i, string) Erlang 1
[string
Objective-C (NSString * only) 0
characterAtIndex:i]
string.sub(string, i, i)
Lua1 1
(string):sub(i, i)
string index string i Tcl 0

1. In this language, the index can be negative, which then indicates the number of places
before the end of the string.

# Example in ALGOL 68 #
"Hello, World"[2]; // 'e'
// Example in C#
"Hello, World"[2]; // 'l'
# Examples in Python
"Hello, World"[2] # 'l'
"Hello, World"[-3] # 'r'
' Example in Visual Basic
GetChar("Hello, World", 2) ' "e"
' Example in Visual Basic .NET
"Hello, World".Chars(2) ' "l"c

[edit] Compare (integer result)

Definition compare(string1,string2) returns integer.


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
Description 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
IF string1<string2 THEN -1 ELSE ABS ALGOL 68
(string1>string2) FI
cmp(string1, string2) Python (before 3.0 only)
strcmp(string1, string2) C, C++ (char * only), PHP
std.string.cmp(string1, string2) D
StrComp(string1, string2) VB
string1 cmp string2 Perl
string1 <=> string2 Ruby
string1.compare(string2) C++ (std::string only)
compare(string1, string2) REXX
CompareStr(string1, string2) Pascal, Object Pascal (Delphi)
string1.compareTo(string2) Java
string1.CompareTo(string2) VB .NET, C#, F#
(string= string1 string2) Common Lisp
(string-compare string1 string2 p< p= p>) Scheme (SRFI 13)
compare string1 string2 OCaml
Standard ML (returns LESS, EQUAL, or
String.compare (string1, string2)
GREATER)
compare string1 string2 Haskell (returns LT, EQ, or GT)
[string]::Compare(string1, string2) Windows PowerShell
[string1 compare:string2] Objective-C (NSString * only)
LLT(string1,string2)
LLE(string1,string2)
Fortran (returns .TRUE. or .FALSE.)
LGT(string1,string2)
LGE(string1,string2)
string1.localeCompare(string2) JavaScript
bytes.Compare([]byte(string1), []byte(string2)) Go
string compare ?-nocase? ?-length int? string1
Tcl
string2

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

[edit] Compare (relational operator-based, Boolean result)

Definition string1 op string2 OR (compare string1 string2) returns Boolean.


Lexicographically compares two strings using a relational operator or function.
Description
Boolean result returned.
Format Languages
Pascal, Object Pascal (Delphi),
string1 op string2, where op can be any of =, <>, <, >, <= and
OCaml, Standard ML, VB,
>=
VB .NET, F#
string1 op string2, where op can be any of =, /=, ≠, <, >, <=, ≤
ALGOL 68
and ≥; Also: EQ, NE, LT, LE, GE and GT
(stringX? string1 string2), where X can be any of =, -ci=, <,
-ci<, >, -ci>, <=, -ci<=, >= and -ci>= (operators starting with '- Scheme
ci' are case-insensitive)
(stringX string1 string2), where X can be any of =, -ci=, <>,
-ci<>, <, -ci<, >, -ci>, <=, -ci<=, >= and -ci>= (operators Scheme (SRFI 13)
starting with '-ci' are case-insensitive)
(stringX string1 string2), where X can be any of =, -equal, /=,
-not-equal, <, -lessp, >, -greaterp, <=, -not-greaterp, >= and Common Lisp
-not-lessp (the verbal operators are case-insensitive)
string1 op string2, where op can be any of =, \=, <, >, <= and
REXX
>=
string1 op string2, where op can be any of =, /=, <, >, <= and
Ada
>=
string1 op string2, where op can be any of ==, /=, <, >, =< and
Erlang
>=
string1 op string2, where op can be any of ==, /=, <, >, <= and
Haskell
>=
string1 op string2, where op can be any of eq, ne, lt, gt, le and
Perl
ge
C++ (std::string only), C#, D,
string1 op string2, where op can be any of ==, !=, <, >, <= and
Go, JavaScript, Python, PHP,
>=
Ruby
string1 op string2, where op can be any of -eq, -ceq, -ne, -cne,
-lt, -clt, -gt, -cgt, -le, -cle, -ge, and -cge (operators starting with Windows PowerShell
'c' are case-sensitive)
string1 op string2, where op can be any of ==, ~=, <, >, <= and
Lua
>=
string1 op string2, where op can be any of ==, /=, <, >, <= and
Fortran
>=; Also: .EQ., .NE., .LT., .LE., .GT. and .GE.
% Example in Erlang
"hello" > "world". % returns false
# Example in Windows PowerShell
"hello" -gt "world" # returns false
;; Example in common lisp
(string> "art" "painting") ; returns nil
(string< "art" "painting") ; returns non nil

[edit] Concatenation

Main article: Concatenation


Definition concatenate(string1,string2) returns string.
Concatenates (joins) two strings to each other, returning the combined string. Note
Description 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 Ada, VB, VB .NET
C, C++ (char * only; modifies string1, which must have
strcat(string1, string2)
enough space to store the result)
string1 . string2 Perl, PHP
ALGOL 68, C++ (std::string only), C#, Go, Pascal,
string1 + string2 Object Pascal (Delphi), Java, JavaScript, Windows
PowerShell, Python, Ruby, F#, Turing
string1 ~ string2 D
(string-append string1 string2) Scheme
(concatenate 'string string1 string2) Common Lisp
string1 || string2 REXX, SQL
string1 // string2 Fortran
string1 ++ string2 Erlang, Haskell
string1 ^ string2 OCaml, Standard ML, F#
[string1
Objective-C (NSString * only)
stringByAppendingString:string2]
string1 .. string2 Lua
// Example in C#
"abc" + "def"; // returns "abcdef"
' Example in Visual Basic
"abc" & "def" ' returns "abcdef"
// Example in D
"abc" ~ "def"; // returns "abcdef"
;; Example in common lisp
(concatenate 'string "abc " "def " "ghi") ; returns "abc def ghi"

[edit] Contains

Definition contains(string,substring) returns boolean


Returns whether string contains substring as a substring. This is equivalent to using
#Find and then detecting that it does not result in the failure condition listed in the
Description
third column of the #Find section. However, some languages have a simpler way of
expressing this test.
Related
Format Languages
string_in_string(string, loc int, substring) ALGOL 68
ContainsStr(string, substring) Delphi
string.Contains(substring) C#, VB .NET, Windows PowerShell, F#
string.contains(substring) Java (1.5+)
string.indexOf(substring) >= 0 JavaScript
substring in string Python (2.3+)
string.include?(substring) Ruby
Data.List.isInfixOf substring string Haskell (GHC 6.6+)
¢ Example in ALGOL 68 ¢
string in string("e", loc int, "Hello mate"); ¢ returns true ¢
string in string("z", loc int, "word"); ¢ returns false ¢
// Example In C#
"Hello mate".Contains("e"); // returns true
"word".Contains("z"); // returns false
# Example in Python
"e" in "Hello mate" # returns true
"z" in "word" # returns false

[edit] 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 programmer but is
often a significantly more expensive operation, this is especially true when using "C-strings".

Format Languages
Python, C++(std::string only), C# Go, JavaScript, PHP,
string1 == string2
Ruby, Erlang, Haskell, Lua, D
string1 == string2 or
Fortran
string1 .EQ. string2
strcmp(string1, string2) == 0 C, C++ (char * only)
(string=? string1 string2) Scheme
(string= string1 string2) Common Lisp
ALGOL 68, Ada, Object Pascal (Delphi), OCaml, Pascal,
string1 = string2
REXX, Standard ML, VB, VB .NET, F#
test string1 = string2, or
Bourne Shell
[ string1 = string2 ]
string1 eq string2 Perl
string1.equals(string2) Java
string1 -eq string2, or
[string]::Equals(string1, Windows PowerShell
string2)
[string1
isEqualToString:string2], or Objective-C (NSString * only)
[string1 isEqual:string2]
// Example in C#
"hello" == "world" // returns false
' Example in Visual Basic
"hello" = "world" ' returns false
# Example in Windows PowerShell
"hello" -eq "world" # returns false

[edit] Find
Definition find(string,substring) returns integer
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
Description
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
returns BOOL: TRUE or
string in string(substring, pos, string[startpos:]) ALGOL 68 FALSE, and position in
REF INT pos.
VB (positions start at
InStr(«startpos,»string,substring) returns 0
1)
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
C, C++ (char *
strstr(string, substring) only, returns pointer returns NULL
to first character)
std.string.find(string, substring) D returns -1
strings.Index(string, substring) Go returns -1
Pascal, Object Pascal
pos(substring, string) returns 0
(Delphi)
pos(substring, string«,startpos») REXX returns 0
C++ (std::string
string.find(substring«,startpos») returns std::string::npos
only)
string.find(substring«,startpos«,endpos»») returns -1
Python
string.index(substring«,startpos«,endpos»») raises ValueError
string.index(substring«,startpos») Ruby returns nil
string.indexOf(substring«,startpos») Java, JavaScript returns -1
VB .NET, C#,
string.IndexOf(substring«,startpos«,
Windows returns -1
charcount»»)
PowerShell, F#
string:str(string, substring) Erlang returns 0
(string-contains string substring) Scheme (SRFI 13) returns #f
(search substring string) Common Lisp returns NIL
List.findIndex (List.isPrefixOf substring) Haskell (returns Just
returns Nothing
(List.tails string) index)
Str.search_forward (Str.regexp_string substring)
OCaml raises Not_found
string 0
Substring.size (#1 (Substring.position substring
Standard ML returns string length
(Substring.full string)))
[string rangeOfString:substring].location Objective-C returns NSNotFound
(NSString * only)
string.find(string, substring)
Lua returns nil
(string):find(substring)
returns 0 if substring is not
startpos = INDEX(string, substring «,back» «, in string; returns
Fortran
kind») LEN(string)+1 if substring
is empty
returns 0 (positions start at
POSITION(substring IN string) SQL
1)
; Examples in Common Lisp
(search "e" "Hello mate") ; returns 1
(search "z" "word") ; returns NIL
// Examples in C#
"Hello mate".IndexOf("e"); // returns 1
"Hello mate".IndexOf("e", 4); // returns 9
"word".IndexOf("z"); // returns -1
; Examples in Scheme
(use-modules (srfi srfi-13))
(string-contains "Hello mate" "e") ; returns 1
(string-contains "word" "z") ; returns #f
' Examples in Visual Basic
InStr("Hello mate", "e") ' returns 2
InStr(5, "Hello mate", "e") ' returns 10
InStr("word", "z") ' returns 0

[edit] Find character

Definition find character(string,char) returns integer


Returns the position of the start of the first occurrence of the character char in string.
If the character 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
Description interpreted as Boolean FALSE. This can be accomplished as a special case of #Find,
with a string of one character; but it may be simpler or more efficient in many
languages to locate just a single character. Also, in many languages, characters and
strings are different types, so it is convenient to have such a function.
Related find
Format Languages If not found
returns BOOL: TRUE or
char in string(char, pos,
ALGOL 68 FALSE, and position in REF
string[startpos:])
INT pos.
C, C++ (char * only,
strchr(string,char) returns pointer to returns NULL
character)
std.string.find(string, dchar) D returns -1
string.find(char«,startpos») C++ (std::string only) returns std::string::npos
string.indexOf(char«,startpos») Java returns -1
string.IndexOf(char«,startpos«, VB .NET, C#, Windows returns -1
charcount»») PowerShell, F#
(position char string) Common Lisp returns NIL
Haskell (returns Just
List.elemIndex char string returns Nothing
index)
String.index string char OCaml raises Not_found
position = SCAN (string, set «, back»
«, kind») or
Fortran returns zero
position = VERIFY (string, set «,
back» «, kind»)[a]
// Examples in C#
"Hello mate".IndexOf('e'); // returns 1
"word".IndexOf('z') // returns -1
; Examples in Common Lisp
(position #\e "Hello mate") ; returns 1
(position #\z "word") ; returns NIL

^a Given a set of characters, SCAN returns the position of the first character found[1], while
VERIFY returns the position of the first character that does not belong to the set [2].

[edit] Format

Definition format(formatstring, items) returns string


Returns the formatted string representation of one or more items. See sprintf for more
Description
information.
Format Languages
associate(file, string); putf(file, $formatstring$,
ALGOL 68
items)
Format(item, formatstring) VB
sprintf(formatstring, items) Perl, PHP, Ruby
io_lib:format(formatstring, items) Erlang
sprintf(outputstring, formatstring, items) C, C++ (char * only)
std.string.format(formatstring, items) D
Format(formatstring, items) Delphi
fmt.Sprintf(formatstring, items) Go
printf -v outputstring formatstring items Unix
formatstring % (items) Python, Ruby
Python 3.x (format specification is different
formatstring.format(items)
from printf)
Printf.sprintf formatstring items OCaml, F#
Text.Printf.printf formatstring items Haskell (GHC)
String.format(formatstring, items) Java
String.Format(formatstring, items) VB .NET, C#, F#
(format formatstring items) Scheme (SRFI 28)
(format nil formatstring items) Common Lisp
formatstring -f items Windows PowerShell
[NSString stringWithFormat:formatstring, items] Objective-C (NSString * only)
string.format(formatstring, items)
Lua
(formatstring):format(items)
WRITE (outputstring, formatstring) items Fortran
// Example in C#
String.Format("My {0} costs {1:C2}", "pen", 19.99); // returns "My pen costs
$19.99"
// Example in Delphi
Format('My %s costs $%2f', ['pen', 19.99]); // returns "My pen costs
$19.99"
// Example in Java
String.format("My %s costs $%2f", "pen", 19.99); // returns "My pen costs
$19.99"
# Example in Python
"My %s costs $%2f" % ("pen", 19.99); # returns "My pen costs
$19.99"
"My {0} costs ${1:2f}".format("pen", 19.99); # returns "My pen costs
$19.99"
; Example in Scheme
(format "My ~a costs $~1,2F" "pen" 19.99) ; returns "My pen costs
$19.99"

[edit] Inequality

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

Format Languages
string1 ne string2, or string1 NE ALGOL 68 - note: the operator "ne" is literally in bold
string2 type-font.
string1 /= string2 ALGOL 68, Ada, Erlang, Fortran, Haskell
VB, VB .NET, Pascal, Object Pascal (Delphi), OCaml,
string1 <> string2
Standard ML, F#
string1 ne string2 Perl
(string<> string1 string2) Scheme (SRFI 13)
(string/= string1 string2) Common Lisp
C++ (std::string only), C#, Go, JavaScript, Python,
string1 != string2
Ruby, D
string1 \= string2 REXX
test string1 != string2, or
Bourne Shell
[ string1 != string2 ]
string1 -ne string2, or
Windows PowerShell
-not [string]::Equals(string1, string2)
string1 ~= string2 Lua
// Example in C#
"hello" != "world" // returns true
' Example in Visual Basic
"hello" <> "world" ' returns true
# Example in Windows PowerShell
"hello" -ne "world" # returns true

[edit] index

see #Find

[edit] indexof

see #Find

[edit] instr

see #Find

[edit] instrrev

see #rfind

[edit] join

Definition join(separator, list_of_strings) joins a list of strings with a separator


Joins the list of strings into a new string, with the separator string between each of
Description
the substrings. Opposite of split.
Related sprintf
Format Languages
std.string.join(array_of_strings, separator) D
string:join(list_of_strings, separator) Erlang (programming language)
join(separator, list_of_strings) Perl, PHP
implode(separator, array_of_strings) PHP
separator.join(sequence_of_strings) Python
array_of_strings.join(separator) Ruby, JavaScript
(string-join array_of_strings separator) Scheme (SRFI 13)
(format nil "~{~a~^separator~}" array_of_strings) Common Lisp
strings.Join(array_of_strings, separator) Go
String.concat separator list_of_strings OCaml
String.concatWith separator list_of_strings Standard ML
Data.List.intercalate separator list_of_strings Haskell (GHC 6.8+)
Join(array_of_strings, separator) VB
String.Join(separator, array_of_strings) VB .NET, C#, F#
&{$OFS=$separator; "$array_of_strings"}, or
Windows PowerShell
array_of_strings -join separator
[array_of_strings componentsJoinedByString:separator] Objective-C (NSString * only)
table.concat(table_of_strings, separator) Lua
// Example in C#
String.Join("-", {"a", "b", "c"}) // "a-b-c"
# Example in Perl
join( '-', ('a', 'b', 'c')); # 'a-b-c'
# Example in Python
"-".join(["a", "b", "c"]) # 'a-b-c'
# Example in Ruby
["a", "b", "c"].join("-") # 'a-b-c'
; Example in Scheme
(use-modules (srfi srfi-13))
(string-join '("a" "b" "c") "-") ; "a-b-c"

[edit] lastindexof

see #rfind

[edit] left

Definition left(string,n) returns string


Returns the left n part of a string. If n is greater than the length of the string then
Description
most implementations return the whole string (exceptions exist - see code examples).
Format Languages
string (string'First ..
Ada
string'First + n - 1)
string:substr(string, start,
Erlang
length)
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[0..n - 1]
string.substr(0,n) C++ (std::string only)
D (if n is larger than the length of the string, then in Debug mode
string[0 .. n] ArrayRangeException is thrown, in Release mode, the behaviour is
unspecified)
string.Substring(0,n) VB .NET, C#, Windows PowerShell, F#
leftstr(string, n) Pascal, Object Pascal (Delphi)
string.substring(0,n) Java, JavaScript
(string-take string n) Scheme (SRFI 13)
take n string Haskell
[string substringToIndex:n] Objective-C (NSString * only)
String.extract (string, n,
Standard ML
NONE)
String.sub string 0 n OCaml (if n is larger than length of string, raises Invalid_argument)
string.[..n] F#
string.sub(string, 1, n)
Lua
(string):sub(1, n)
string(:n) Fortran
/* Examples in REXX */
left("abcde", 3) /* returns "abc" */
left("abcde", 8) /* returns "abcde " */
left("abcde", 8, "*") /* returns "abcde***" */
; Examples in Scheme
(use-modules (srfi srfi-13))
(string-take "abcde", 3) ; returns "abc"
(string-take "abcde", 8) ; error
' Examples in Visual Basic
Left("sandroguidi", 3) ' returns "san"
Left("sandroguidi", 100) ' returns "sandroguidi"

[edit] len

see #length

[edit] length

Definition length(string) returns an integer number


Returns the length of a string (not counting the null terminator or any other of the
Description
string's internal structural information). An empty string returns a length of 0.
Format Returns Languages
string'Length Ada
UPB string ALGOL 68
Perl, Ingres, Pascal, Object Pascal
length(string)
(Delphi), REXX, SQL
len(string) Python, Go
length(string),
Erlang
string:len(string)
Len(string) VB
Number of 16-bit UTF-16- VB .NET, C#, Windows PowerShell,
string.Length
encoded blocks F#
Number of bytes (Ruby lacks
string.size OR string.length Ruby
Unicode support)
strlen(string) Number of bytes C, C++ (char * only), PHP
string.length() C++ (std::string only)
string.length D
Number of 16-bit UTF-16-
string.length() Java
encoded blocks
string.length JavaScript
(string-length string) Scheme
(length string) Common Lisp
String.length string OCaml
size string Standard ML
Number of Unicode
length string Haskell
codepoints
[string length] Objective-C (NSString * only)
string.len(string)
(string):len() Lua
#string
LEN(string), or
Fortran
LEN_TRIM(string)
// Examples in C#
"hello".Length; // returns 5
"".Length; // returns 0
# Examples in Erlang
string:len("hello"). % returns 5
string:len(""). % returns 0
# Examples in Perl
length("hello"); # returns 5
length(""); # returns 0
' Examples in Visual Basic
Len("hello") ' returns 5
Len("") ' returns 0
//Examples in Objective c
[@"hello" Length] //returns 5
[@"" Length] //returns 0
-- Examples in Lua
("hello"):len() -- returns 5
#"" -- returns 0

[edit] locate

see #Find

[edit] Lowercase

Definition lowercase(string) returns string


Description Returns the string in lower case.
Format Languages
LCase(string) VB
lc(string) Perl
tolower(char) C (operates on a single character)
std.string.tolower(string) D
C++ (std::string only, result is stored in string result
transform(string.begin(), string.end(),
which is at least as long as string, and may or may not be
result.begin(), tolower)[3]
string itself)
lowercase(string) Delphi
strtolower(string) PHP
echo "string" | tr 'A-Z' 'a-z' Unix
string.lower() Python
Ruby (only ASCII characters as Ruby lacks Unicode
string.downcase
support)
strings.ToLower(string) Go
(string-downcase string) Scheme (R6RS), Common Lisp
String.lowercase string OCaml
String.map Char.toLower string Standard ML
map Char.toLower string Haskell
string.toLowerCase() Java, JavaScript
to_lower(string) Erlang
string.ToLower() VB .NET, C#, Windows PowerShell, F#
[string lowercaseString] Objective-C (NSString * only)
string.lower(string)
Lua
(string):lower()
LOWER(string) SQL
// Example in C#
"Wiki means fast?".ToLower(); // "wiki means fast?"
; Example in Scheme
(use-modules (srfi srfi-13))
(string-downcase "Wiki means fast?") ; "wiki means fast?"
/* Example in C */
#include <ctype.h>
#include <string.h>
#include <stdio.h>
int main(void) {
char string[] = "Wiki means fast?";
int i;
for (i = 0; i < sizeof(string); ++i) {
/* transform characters in place, one by one */
string[i] = tolower(string[i]);
}
puts(string); /* "wiki means fast?" */
return 0;
}

[edit] mid

see #substring

[edit] partition

<string>.partition(separator) returns the sub-string before the separator; the


Definition
separator; then the sub-string after the separator.
Splits the given string by the separator and returns the three substrings that together
Description
make the original.
Format Languages
string.partition(separator) Python, Ruby(1.9+)
lists:partition(pred, string) Erlang
split /(separator)/, string, 2 Perl
# 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 with find occurrences changed to replace.
Format Languages
changestr(find, string, replace) REXX
std.string.replace(string, find, replace) D
Replace(string, find, replace) VB
string.Replace(find, replace) VB .NET, C#, F#
str_replace(find, replace, string) PHP
re:replace(string, find, replace, «{return, list}») Erlang
string.replace(find, replace) Python, Java (1.5+)
string.replaceAll(find_regex, replace)[4] Java
string.gsub(find, replace) Ruby
[4]
string =~ s/find_regex/replace/g Perl
string.replace(find, replace, "g") or
JavaScript
string.replace(/find_regex/g, replace)[4]
echo "string" | sed 's/find_regex/replace/g'[4] Unix
string.replace(find, replace), or
Windows PowerShell
string -replace find_regex, replace[4]
Str.global_replace (Str.regexp_string find) replace string OCaml
[string stringByReplacingOccurrencesOfString:find Objective-C (NSString *
withString:replace] only)
string.gsub(string, find, replace)
Lua
(string):gsub(find, replace)
string map {find replace} string Tcl
// Examples in C#
"effffff".Replace("f", "jump"); // returns "ejumpjumpjumpjumpjumpjump"
"blah".Replace("z", "y"); // returns "blah"
// Examples in Java
"effffff".replace("f", "jump"); // returns "ejumpjumpjumpjumpjumpjump"
"effffff".replaceAll("f*", "jump"); // returns "ejump"
' Examples in Visual Basic
Replace("effffff", "f", "jump") ' returns "ejumpjumpjumpjumpjumpjump"
Replace("blah", "z", "y") ' returns "blah"
# Examples in Windows PowerShell
"effffff" -replace "f", "jump" # returns "ejumpjumpjumpjumpjumpjump"
"effffff" -replace "f*", "jump" # returns "ejump"

[edit] reverse

Definition reverse(string)
Description Reverses the order of the characters in the string.
Format Languages
reverse string Perl, Haskell
lists:reverse(string) Erlang
strrev(string) PHP
string[::-1] Python
(string-reverse string) Scheme (SRFI 13)
(reverse string) Common Lisp
string.reverse Ruby
new StringBuilder(string).reverse().toString() Java
std::reverse(string.begin(), string.end()); C++ (std::string only, modifies string)
StrReverse(string) VB
string.Reverse().ToString() VB .NET, C#
implode (rev (explode string)) Standard ML
string.split("").reverse().join("") JavaScript
string.reverse(string)
Lua
(string):reverse()
# Example in Perl
reverse "hello" # returns "olleh"
# Example in Python
"hello"[::-1] # returns "olleh"
; Example in Scheme
(use-modules (srfi srfi-13))
(string-reverse "hello") ; returns "olleh"

[edit] rfind

Definition rfind(string,substring) returns integer


Returns the position of the start of the last occurrence of substring in string. If the
substring is not found most of these routines return an invalid index value – -1 where
Description
indexes are 0-based, 0 where they are 1-based – or some value to be interpreted as
Boolean FALSE.
Related instr
Format Languages If not found
InStrRev(«startpos,» string,substring) VB returns 0
rindex(string,substring«,startpos») Perl returns -1
strrpos(string,substring«,startpos») PHP returns FALSE
C++ (std::string returns
string.rfind(substring«,startpos»)
only) std::string::npos
std.string.rfind(string, substring) D returns -1
string.rfind(substring«,startpos«, endpos»») Python returns -1
string.rindex(substring«,startpos») Ruby returns nil
strings.LastIndex(string, substring) Go returns -1
string.lastIndexOf(substring«,startpos») Java, JavaScript returns -1
VB .NET, C#,
string.LastIndexOf(substring«,startpos«,
Windows PowerShell, returns -1
charcount»»)
F#
(search substring string :from-end) Common Lisp returns NIL
[string rangeOfString:substring Objective-C (NSString returns
options:NSBackwardsSearch].location * only) NSNotFound
Str.search_backward (Str.regexp_string substring)
OCaml raises Not_found
string (Str.length string - 1)
; Examples in Common Lisp
(search "e" "Hello mate" :from-end) ; returns 9
(search "z" "word" :from-end) ; returns NIL
// Examples in C#
"Hello mate".LastIndexOf("e"); // returns 9
"Hello mate".LastIndexOf("e", 4); // returns 1
"word".LastIndexOf("z"); // returns -1
' Examples in Visual Basic
InStrRev("Hello mate", "e") ' returns 10
InStrRev(5, "Hello mate", "e") ' returns 2
InStrRev("word", "z") ' returns 0

[edit] right

Definition right(string,n) returns string


Returns the right n part of a string. If n is greater than the length of the string then
Description
most implementations return the whole string (exceptions exist - see code examples).
Format Languages
string (string'Last - n + 1 ..
Ada
string'Last)
Right(string,n) VB
right(string,n) Ingres
string.substring(string.length()-n,
Java
string.length())
string.substring(string.length-n,
JavaScript
string.length)
right(string,n «,padchar») REXX, Erlang
substr(string,-n) Perl, PHP
string[-n:] Python
(string-take-right string n) Scheme (SRFI 13)
string[-n..-1] Ruby
D (if n is larger than length of string, then in Debug mode
string[$-n .. $] ArrayRangeException is thrown, and unspecified
behaviour in Release mode)
String.sub string (String.length string OCaml (if n is larger than length of string, raises
- n) n Invalid_argument)
string.sub(string, -n)
Lua
(string):sub(-n)
/* Examples in REXX */
right("abcde", 3) /* returns "cde" */
right("abcde", 8) /* returns " abcde" */
right("abcde", 8, "*") /* returns "***abcde" */
; Examples in Scheme
(use-modules (srfi srfi-13))
(string-take-right "abcde", 3) ; returns "cde"
(string-take-right "abcde", 8) ; error
' Examples in Visual Basic
Right("sandroguidi", 3) ' returns "idi"
Right("sandroguidi", 100) ' returns "sandroguidi"
// Examples in Java; extract rightmost 4 characters
String str = "CarDoor";
str.substring(str.length()-4, str.length()); // returns 'Door'

[edit] rpartition

<string>.rpartition(separator) Searches for the separator from right-to-left within the


Definition string then returns the sub-string before the separator; the separator; then the sub-
string after the separator.
Splits the given string by the right-most separator and returns the three substrings
Description
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')

[edit] slice

see #substring

[edit] split

Definition <string>.split(separator[, limit]) splits a string on separator, optionally only up to a


limited number of substrings
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
Description 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
string:tokens(string, sepchars) Erlang
strings.Split(string, separator, limit) Go
(string-tokenize string« charset« start« end»»») Scheme (SRFI 13)
Split(string, sepchars«, limit») VB
string.Split(sepchars«, limit«, options»») VB .NET, C#, F#
string -split separator«, limit«, options»» Windows PowerShell
Str.split (Str.regexp_string separator) string OCaml
std.string.split(string, separator) D
[string componentsSeparatedByString:separator] Objective-C (NSString * only)
// Example in C#
"abc,defgh,ijk".Split(','); // {"abc", "defgh", "ijk"}
"abc,defgh;ijk".Split(',', ';'); // {"abc", "defgh", "ijk"}
% Example in Erlang
string:tokens("abc;defgh;ijk", ";"). % ["abc", "defgh", "ijk"]
// Examples in Java
"abc,defgh,ijk".split(','); // {"abc", "defgh", "ijk"}
"abc,defgh;ijk".split(',|;'); // {"abc", "defgh", "ijk"}
# Examples in Perl
split(/spam/, 'Spam eggs spam spam and ham'); # ('Spam eggs ', ' ', ' and
ham')
split(/X/, 'Spam eggs spam spam and ham'); # ('Spam eggs spam spam and
ham')

[edit] sprintf

see #Format

[edit] strip

see #trim

[edit] strcmp

see #Compare (integer result)


[edit] substring

substring(string, startpos, endpos) returns string


Definition
substr(string, startpos, numChars) returns string
Returns a substring of string between starting at startpos and endpos, or starting at
startpos of length numChars. The resulting string is truncated if there are fewer than
Description
numChars characters beyond the starting point. endpos represents the index after the
last character in the substring.
Format Languages
string[startpos:endpos] ALGOL 68 (changes base index)
string (startpos .. endpos) Ada (changes base index)
Mid(string, startpos, numChars) VB
AWK (changes string), Perl1,3,
substr(string, startpos, numChars)
PHP1,3
substr(string, startpos «,numChars, padChar») REXX
string[startpos:endpos] Python1,2, Go
string[startpos, numChars]
string[startpos .. endpos-1] Ruby1,2
string[startpos ... endpos]
string.slice(startpos«, endpos») JavaScript1,2
C++ (std::string only),
string.substr(startpos«, numChars»)
JavaScript
VB .NET, C#, Windows
string.Substring(startpos, numChars)
PowerShell, F#
string.substring(startpos«, endpos») Java, JavaScript
copy(string, startpos, numChars) Delphi
(substring string startpos endpos) Scheme
(subseq string startpos endpos) Common Lisp
String.sub string startpos numChars Ocaml
substring (string, startpos, numChars) Standard ML
string:sub_string(string, startpos, endpos)
Erlang
string:substr(string, startpos, numChars)
char result[numChars+1] = "";
C
strncat(result, string + startpos, numChars);
string[startpos .. endpos+1) D
take numChars $ drop startpos string Haskell
[string substringWithRange:NSMakeRange(startpos,
Objective-C (NSString * only)
numChars)]
string.[startpos..endpos] F#
string.sub(string, startpos, endpos)
Lua1,2
(string):sub(startpos, endpos)
string(startpos:endpos) Fortran
SUBSTRING(string FROM startpos «FOR numChars») SQL

1. In this language, startpos can be negative, which indicates to start that number of places
before the end of the string.
2. In this language, endpos can be negative, which indicates to end that number of places
before the end of the string.
3. In this language, numChars can be negative, which indicates to end that number of places
before the end of the string.

// Examples in C#
"abc".Substring(1, 1): // returns "b"
"abc".Substring(1, 2); // returns "bc"
"abc".Substring(1, 6); // error
% Examples in Erlang
string:substr("abc", 2, 1). % returns "b"
string:substr("abc", 2). % returns "bc"
# Examples in Python
"abc"[1:2] # returns "b"
"abc"[1:3] # returns "bc"
/* Examples 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****" */

[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)
std.string.toupper(string) D
C++ (std::string only, result is stored in string result
transform(string.begin(), string.end(),
which is at least as long as string, and may or may not be
result.begin(), toupper)[3]
string itself)
uppercase(string) Delphi
strtoupper(string) PHP
echo "string" | tr 'a-z' 'A-Z' Unix
translate(string) , or REXX

UPPER variables , or
PARSE UPPER VAR SrcVar DstVar
string.upper() Python
Ruby (only ASCII characters as Ruby lacks Unicode
string.upcase
support)
strings.ToUpper(string) Go
(string-upcase string) Scheme, Common Lisp
String.uppercase string OCaml
String.map Char.toUpper string Standard ML
map Char.toUpper string Haskell
string.toUpperCase() Java, JavaScript
to_upper(string) Erlang
string.ToUpper() VB .NET, C#, Windows PowerShell, F#
[string uppercaseString] Objective-C (NSString * only)
string.upper(string)
Lua
(string):upper()
UPPER(string) SQL
// Example in C#
"Wiki means fast?".ToUpper(); // "WIKI MEANS FAST?"
/* Example in REXX */
translate("Wiki means fast?") /* "WIKI MEANS FAST?" */

/* Example #2 */
A='This is an example.'
UPPER A /* "THIS IS AN EXAMPLE." */

/* Example #3 */
A='upper using Translate Function.'
Translate UPPER VAR A Z /* Z="UPPER USING TRANSLATE FUNCTION." */
; Example in Scheme
(use-modules (srfi srfi-13))
(string-upcase "Wiki means fast?") ; "WIKI MEANS FAST?"
' Example in Visual Basic
UCase("Wiki means fast?") ' "WIKI MEANS FAST?"

[edit] 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.

[edit] Notes
1. ^ http://fortranwiki.org/fortran/show/scan
2. ^ http://fortranwiki.org/fortran/show/verify
3. ^ a b The transform, tolower, and toupper functions exist in the std:: namespace.
You must include the <algorithm> header file for the transform function, and include
the <cctype> header file for the tolower and toupper functions. In some compilers, a
cast must be made to the tolower or toupper function in order to resolve a function
overloading ambiguity; e.g. std::transform(string.begin(), string.end(),
result.begin(), (int (*)(int))std::tolower);
4. ^ a b c d e The "find" string in this construct is interpreted as a regular expression. Certain
characters have special meaning in regular expressions. If you want to find a string
literally, you need to quote the special characters.

[edit] External links


 Perl String Functions
 Python String Methods
 Scheme String Procedures
 Erlang String Functions
 .NET String Methods and Properties
 Ruby String Class
 PHP String functions
 java.lang.String members
 Online String Manipulation Tools
 Haskell Hierarchical Libraries
 std.string from Phobos (D standard library)
 Lua String Functions

You might also like