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

DefinitioncharAt returns character.
DescriptionReturns character at index in the string.
EquivalentSee [|substring] of length 1 character.

FormatLanguagesBase
index
stringALGOL 68, APL, Julia, Pascal, Object Pascal, Seed71
stringC, C++, C#, Cobra, D, FreeBASIC, Go, Python, PHP, Ruby, Windows PowerShell, JavaScript, APL0
stringPHP 0
stringAda≥1
MidVB1
MID$BASIC1
string.CharsVB.NET0
stringFortran1
string.charAtJava, JavaScript0
string.OCaml, F#0
string.chars.nthRust0
stringPick Basic1
String.sub Standard ML0
string !! iHaskell0
Scheme0
Common Lisp0
ISLISP0
Clojure0
substrPerl 50
substr
string.substr
Raku0
substrPL/I1
string.atC++ 0
lists:nthErlang1
Objective-C 0
string.sub
:sub
Lua1
string at: iSmalltalk 1
string index string iTcl0
StringTakeMathematica, Wolfram Language1
string@''iEiffel1
string COBOL1
$Bash0
i''⌷stringAPL0 or 1


var
MyStr: string = 'Hello, World';
MyChar: Char;
begin
MyChar := MyStr; // 'e'


  1. Example in ALGOL 68 #
"Hello, World"; // 'e'


// Example in C
  1. include
char myStr1 = "Hello, World";
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'


  1. Example in Perl 5
substr; # 'e'


  1. Examples in Python
"Hello, World" # 'l'
"Hello, World" # 'r'


  1. Example in Raku
"Hello, World".substr; # 'e'


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

Definitioncompare returns integer.
DescriptionCompares 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.


  1. Example in Perl 5
"hello" cmp "world"; # returns -1


  1. Example in Python
cmp # returns -1


  1. Examples in Raku
"hello" cmp "world"; # returns Less
"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)

FormatLanguages
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 equivalentsCOBOL
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, leRust


% Example in Erlang
"hello" > "world". % returns false


  1. Example in Raku
"art" gt "painting"; # returns False
"art" lt "painting"; # returns True


  1. Example in Windows PowerShell
"hello" -gt "world" # returns false


;; Example in Common Lisp
; returns nil
; returns non nil