Comparison of Programming Languages
Comparison of Programming Languages
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
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.
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]
[edit] CharAt
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
# 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] Concatenation
[edit] Contains
[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
^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
[edit] Inequality
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
[edit] lastindexof
see #rfind
[edit] left
[edit] len
see #length
[edit] length
[edit] locate
see #Find
[edit] Lowercase
[edit] mid
see #substring
[edit] partition
[edit] replace
[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
[edit] right
[edit] rpartition
[edit] slice
see #substring
[edit] split
[edit] sprintf
see #Format
[edit] strip
see #trim
[edit] strcmp
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
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
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.