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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Comparison of programming languages (string functions)From Wikipedia, the free encyclopediaJump 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.
string[i]C, C++, C#, D, Go, Python1, PHP, Ruby1, Windows PowerShell
0
string{i} PHP (deprecated in 5.3) 0string(i) Ada ≥1Mid(string,i,1) VB 1string.Chars(i) VB.NET 0string(i:i) Fortran 1string.charAt(i) Java, JavaScript 0string.[i] OCaml, F# 0String.sub (string, i) Standard ML 0string !! i Haskell 0(string-ref string i) Scheme 0(char string i) Common Lisp 0substr(string, i, 1) Perl1 0string.at(i) C++ (std::string only) (w/ bounds checking) 0lists:nth(i, string) Erlang 1[string characterAtIndex:i]
Objective-C (NSString * only) 0
string.sub(string, i, i)(string):sub(i, i)
Lua1 1
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 BasicGetChar("Hello, World", 2) ' "e"' Example in Visual Basic .NET"Hello, World".Chars(2) ' "l"c
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
IF string1<string2 THEN -1 ELSE ABS (string1>string2) FI
# Example in Pythoncmp("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)
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 >=
string1 op string2, where op can be any of =, /=, ≠, <, >, <=, ≤ and ≥; Also: EQ, NE, LT, LE, GE and GT
ALGOL 68
(stringX? string1 string2), where X can be any of =, -ci=, <, -ci<, >, -ci>, <=, -ci<=, >= and -ci>= (operators starting with '-ci' are case-insensitive)
Scheme
(stringX string1 string2), where X can be any of =, -ci=, <>, -ci<>, <, -ci<, >, -ci>, <=, -ci<=, >= and -ci>= (operators starting with '-ci' are case-insensitive)
Scheme (SRFI 13)
(stringX string1 string2), where X can be any of =, -equal, /=, -not-equal, <, -lessp, >, -greaterp, <=, -not-greaterp, >= and -not-lessp (the verbal operators are case-insensitive)
Common Lisp
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 ge
Perl
string1 op string2, where op can be any of ==, !=, <, >, <= and >=
C++ (std::string only), C#, D, 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 'c' are case-sensitive)
Windows PowerShell
string1 op string2, where op can be any of ==, ~=, <, >, <= and >=
Lua
string1 op string2, where op can be any of ==, /=, <, >, <= and >=; Also: .EQ., .NE., .LT., .LE., .GT. and .GE.
Fortran
% 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: ConcatenationDefinition concatenate(string1,string2) returns string.
DescriptionConcatenates (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 Languagesstring1 & string2 Ada, VB, VB .NET
strcat(string1, string2)C, C++ (char * only; modifies string1, which must have enough space to store the result)
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"
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 third column of the #Find section. However, some languages have a simpler way of expressing this test.
RelatedFormat Languages
string_in_string(string, loc int, substring) ALGOL 68ContainsStr(string, substring) Delphistring.Contains(substring) C#, VB .NET, Windows PowerShell, F#string.contains(substring) Java (1.5+)
string.indexOf(substring) >= 0 JavaScriptsubstring in string Python (2.3+)string.include?(substring) RubyData.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".
// 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
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 instrrevFormat Languages If not found
string in string(substring, pos, string[startpos:]) ALGOL 68returns BOOL: TRUE or FALSE, and position in REF INT pos.
InStr(«startpos,»string,substring)VB (positions start at 1)
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 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 findFormat Languages If not found
char in string(char, pos, string[startpos:])
ALGOL 68returns BOOL: TRUE or FALSE, and position in REF INT pos.
strchr(string,char)C, C++ (char * only, returns pointer to character)
returns NULL
std.string.find(string, dchar) D returns -1string.find(char«,startpos») C++ (std::string only) returns std::string::npos
// 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].
table.concat(table_of_strings, separator) Lua// Example in C#String.Join("-", {"a", "b", "c"}) // "a-b-c"# Example in Perljoin( '-', ('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
DescriptionReturns 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 Languagesstring (string'First .. string'First + n - 1)
string[0 .. n]D (if n is larger than the length of the string, then in Debug mode 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, NONE)
Standard ML
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)(string):sub(1, n)
Definition length(string) returns an integer number
DescriptionReturns 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 Returns Languagesstring'Length AdaUPB string ALGOL 68
// Examples in C#"hello".Length; // returns 5"".Length; // returns 0# Examples in Erlangstring:len("hello"). % returns 5string:len(""). % returns 0# Examples in Perllength("hello"); # returns 5length(""); # returns 0' Examples in Visual BasicLen("hello") ' returns 5Len("") ' 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 stringDescription Returns the string in lower case.
Format LanguagesLCase(string) VBlc(string) Perltolower(char) C (operates on a single character)std.string.tolower(string) Dtransform(string.begin(), string.end(), result.begin(), tolower)[3]
C++ (std::string only, result is stored in string result which is at least as long as string, and may or may not be
string.downcaseRuby (only ASCII characters as Ruby lacks Unicode support)
strings.ToLower(string) Go(string-downcase string) Scheme (R6RS), Common LispString.lowercase string OCamlString.map Char.toLower string Standard MLmap Char.toLower string Haskellstring.toLowerCase() Java, JavaScriptto_lower(string) Erlangstring.ToLower() VB .NET, C#, Windows PowerShell, F#[string lowercaseString] Objective-C (NSString * only)string.lower(string)(string):lower()
Lua
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
Definition <string>.partition(separator) returns the sub-string before the separator; the
separator; then the sub-string after the separator.
DescriptionSplits the given string by the separator and returns the three substrings that together make the original.
Format Languagesstring.partition(separator) Python, Ruby(1.9+)lists:partition(pred, string) Erlangsplit /(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 stringDescription Returns a string with find occurrences changed to replace.
Definition reverse(string)Description Reverses the order of the characters in the string.
Format Languagesreverse string Perl, Haskelllists:reverse(string) Erlangstrrev(string) PHPstring[::-1] Python(string-reverse string) Scheme (SRFI 13)(reverse string) Common Lispstring.reverse Rubynew StringBuilder(string).reverse().toString() Javastd::reverse(string.begin(), string.end()); C++ (std::string only, modifies string)StrReverse(string) VBstring.Reverse().ToString() VB .NET, C#implode (rev (explode string)) Standard MLstring.split("").reverse().join("") JavaScriptstring.reverse(string)(string):reverse()
Lua
# Example in Perlreverse "hello" # returns "olleh"# Example in Python"hello"[::-1] # returns "olleh"; Example in Scheme(use-modules (srfi srfi-13))(string-reverse "hello") ; returns "olleh"
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 indexes are 0-based, 0 where they are 1-based – or some value to be interpreted as Boolean FALSE.
DescriptionReturns 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 Languagesstring (string'Last - n + 1 .. string'Last)
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.
DescriptionSplits the given string by the right-most separator and returns the three substrings that together make the original.
Format Languagesstring.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
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 Languagessplit(/separator/, string«, limit») Perlexplode(separator, string«, limit») PHPstring.split(separator«, limit»]) Javascript, Java, Python, Rubystring:tokens(string, sepchars) Erlangstrings.Split(string, separator, limit) Go(string-tokenize string« charset« start« end»»») Scheme (SRFI 13)Split(string, sepchars«, limit») VBstring.Split(sepchars«, limit«, options»») VB .NET, C#, F#string -split separator«, limit«, options»» Windows PowerShellStr.split (Str.regexp_string separator) string OCamlstd.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 Erlangstring: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 Perlsplit(/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')
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 numChars characters beyond the starting point. endpos represents the index after the last character in the substring.
Format Languagesstring[startpos:endpos] ALGOL 68 (changes base index)string (startpos .. endpos) Ada (changes base index)Mid(string, startpos, numChars) VB
Definition uppercase(string) returns stringDescription Returns the string in upper case.
Format LanguagesUCase(string) VBtoupper(string) AWK (changes string)uc(string) Perltoupper(char) C (operates on a single character)std.string.toupper(string) D
PARSE UPPER VAR SrcVar DstVarstring.upper() Python
string.upcaseRuby (only ASCII characters as Ruby lacks Unicode support)
strings.ToUpper(string) Go(string-upcase string) Scheme, Common LispString.uppercase string OCamlString.map Char.toUpper string Standard MLmap Char.toUpper string Haskellstring.toUpperCase() Java, JavaScriptto_upper(string) Erlangstring.ToUpper() VB .NET, C#, Windows PowerShell, F#[string uppercaseString] Objective-C (NSString * only)string.upper(string)(string):upper()
Lua
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 BasicUCase("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.
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