Comparison of programming languages (string functions)
String functions are used in computer programming languages to manipulate a string or query information about a string.
Most 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, therefore all list-manipulation procedures could be considered string functions. However such languages may implement a subset of explicit string-specific functions as well.
For function that manipulate strings, modern object-oriented languages, like C# and Java have immutable strings and return a copy, while others, like C manipulate the original string unless the programmer copies data to a new string. See for example [|Concatenation] below.
The most basic example of a string function is the
length 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. The below list of common functions aims to help limit this confusion.
Common string functions (multi language reference)
String functions common to many languages are listed below, including the different names used. 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.CharAt
| Definition | charAt returns character. |
| Description | Returns character at index in the string. |
| Equivalent | See [|substring] of length 1 character. |
| Format | Languages | Base index |
string | ALGOL 68, APL, Julia, Pascal, Object Pascal, Seed7 | 1 |
string | C, C++, C#, Cobra, D, FreeBASIC, Go, Python, PHP, Ruby, Windows PowerShell, JavaScript, APL | 0 |
string | PHP | 0 |
string | Ada | ≥1 |
Mid | VB | 1 |
MID$ | BASIC | 1 |
string.Chars | VB.NET | 0 |
string | Fortran | 1 |
string.charAt | Java, JavaScript | 0 |
string. | OCaml, F# | 0 |
string.chars.nth | Rust | 0 |
string | Pick Basic | 1 |
String.sub | Standard ML | 0 |
string !! i | Haskell | 0 |
| Scheme | 0 |
| Common Lisp | 0 |
| ISLISP | 0 |
| Clojure | 0 |
substr | Perl 5 | 0 |
substrstring.substr | Raku | 0 |
substr | PL/I | 1 |
string.at | C++ | 0 |
lists:nth | Erlang | 1 |
| Objective-C | 0 |
string.sub:sub | Lua | 1 |
string at: i | Smalltalk | 1 |
string index string i | Tcl | 0 |
StringTake | Mathematica, Wolfram Language | 1 |
string@''i | Eiffel | 1 |
string | COBOL | 1 |
$ | Bash | 0 |
i''⌷string | APL | 0 or 1 |
var
MyStr: string = 'Hello, World';
MyChar: Char;
begin
MyChar := MyStr; // 'e'
"Hello, World"; // 'e'
- Example in ALGOL 68 #
// Example in C
- include
printf; // 'e'
printf; // 'W'
printf; // 'd'
printf; // 'Hello, World'
printf, World; // 'Hello, World'
import std;
using std::string;
char myStr1 = "Hello, World";
string myStr2 = "Hello, World";
std::println, World; // 'Hello, World'
std::println; // '2'
std::println; // ''
// Example in C#
"Hello, World"; // 'l'
- Example in Perl 5
- Examples in Python
"Hello, World" # 'r'
- Example in Raku
' Example in Visual Basic
Mid
' Example in Visual Basic.NET
"Hello, World".Chars ' "l"c
" Example in Smalltalk "
'Hello, World' at: 2. "$e"
//Example in Rust
"Hello, World".chars.nth; // Some
Compare (integer result)
| Definition | compare 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, and Smalltalk which answer a comparison code telling how the receiver sorts relative to string parameter. |
- Example in Perl 5
- Example in Python
- Examples in Raku
"world" cmp "hello"; # returns More
"hello" cmp "hello"; # returns Same
/** Example in Rexx */
compare /* returns index of mismatch: 1 */
; Example in Scheme
; returns index of mismatch: 0
Compare (relational operator-based, Boolean result)
| Format | Languages |
string1 OP string2, where OP can be any of and | Pascal, Object Pascal, OCaml, Seed7, Standard ML, BASIC, VB, VB.NET, F# |
string1 OP string2, where OP can be any of and ; Also: and | ALGOL 68 |
, where OP can be any of and | Scheme |
, where OP can be any of and | Scheme |
, where OP can be any of and | Common Lisp |
, where OP can be any of and | ISLISP |
string1 OP string2, where OP can be any of and | Rexx |
string1 OP string2, where OP can be any of and | PL/I |
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 and | Perl, Raku |
string1 OP string2, where OP can be any of and | C++, C#, D, Go, JavaScript, Python, PHP, Ruby, Rust, Swift |
string1 OP string2, where OP can be any of and | Windows PowerShell |
string1 OP string2, where OP can be any of and | Lua |
string1 OP string2, where OP can be any of and | Smalltalk |
string1 OP string2, where OP can be any of and and | Fortran. |
string1 OP string2 where OP can be any of as well as worded equivalents | COBOL |
string1 OP string2 where OP can be any of and | Cobra |
string1 OP string2 is available in the syntax, but means comparison of the pointers pointing to the strings, not of the string contents. Use the Compare function. | C, Java |
string1.METHOD where METHOD is any of eq, ne, gt, lt, ge, le | Rust |
% Example in Erlang
"hello" > "world". % returns false
- Example in Raku
"art" lt "painting"; # returns True
- Example in Windows PowerShell
;; Example in Common Lisp
; returns nil
; returns non nil