String structure
June 6, 2026 · View on GitHub
The String structure provides the string type and a comprehensive
set of operations for constructing, examining, searching, and converting
strings.
Specified by the Standard ML Basis Library.
Synopsis
type string type char val maxSize : int val size : string -> int val sub : string * int -> char val extract : string * int * int option -> string val substring : string * int * int -> string val ^ : string * string -> string val concat : string list -> string val concatWith : string -> string list -> string val str : char -> string val implode : char list -> string val explode : string -> char list val map : (char -> char) -> string -> string val translate : (char -> string) -> string -> string val tokens : (char -> bool) -> string -> string list val fields : (char -> bool) -> string -> string list val isPrefix : string -> string -> bool val isSubstring : string -> string -> bool val isSuffix : string -> string -> bool val compare : string * string -> order val collate : (char * char -> order) -> string * string -> order val < : string * string -> bool val <= : string * string -> bool val > : string * string -> bool val >= : string * string -> bool val = : string * string -> bool val <> : string * string -> bool
type string
is the type of character strings.
type char
maxSize
maxSize is the longest allowed size of a string.
size
size s (or s.size ()) returns |s|, the number of characters in string s.
sub
sub (s, i) (or s.sub i) returns the i(th) character of s, counting from
zero. This raises Subscript if i < 0 or |s| ≤ i.
extract
extract (s, i, NONE) (or s.extract (i, NONE)) and "extract (s, i, SOME j)"
return substrings
of s. The first returns the substring of s from the i(th)
character to the end of the string, i.e., the string
s[i..|s|-1]. This raises Subscript if i < 0 or |s| < i.
The second form returns the substring of size j starting at
index i, i.e., the string s[i..i+j-1]. Raises Subscript if
i < 0 or j < 0 or |s| < i + j. Note that, if defined,
extract returns the empty string when i = |s|.
substring
substring (s, i, j) (or s.substring (i, j)) returns the substring s[i..i+j-1], i.e.,
the substring of size j starting at index i. This is equivalent to
extract(s, i, SOME j).
^
s ^ t is the concatenation of the strings s and t. This raises
Size if |s| + |t| > maxSize.
concat
concat l is the concatenation of all the strings in l. This raises
Size if the sum of all the sizes is greater than maxSize.
concatWith
concatWith s l returns the concatenation of the strings in the list
l using the string s as a separator. This raises Size if the
size of the resulting string would be greater than maxSize.
str
str c is the string of size one containing the character c.
implode
implode l generates the string containing the characters in the list
l. This is equivalent to concat (List.map str l). This raises
Size if the resulting string would have size greater than maxSize.
explode
explode s (or s.explode ()) is the list of characters in the string s.
map
map f s applies f to each element of s from left to right,
returning the resulting string. It is equivalent to implode(List.map f (explode s)).
translate
translate f s returns the string generated from s by mapping each
character in s by f. It is equivalent to concat(List.map f (explode s)).
tokens
tokens f s returns a list of tokens derived from s from left to
right. A token is a non-empty maximal substring of s not containing
any delimiter. A delimiter is a character satisfying the predicate
f.
Two tokens may be separated by more than one delimiter, whereas
two fields are separated by exactly one delimiter. For example, if
the only delimiter is the character #"|", then the string
"|abc||def" contains two tokens "abc" and "def", whereas it
contains the four fields "", "abc", "" and "def".
fields
fields f s returns a list of fields derived from s from left to
right. A field is a (possibly empty) maximal substring of s not
containing any delimiter. A delimiter is a character satisfying the
predicate f.
Two tokens may be separated by more than one delimiter, whereas
two fields are separated by exactly one delimiter. For example, if
the only delimiter is the character #"|", then the string
"|abc||def" contains two tokens "abc" and "def", whereas it
contains the four fields "", "abc", "" and "def".
isPrefix
isPrefix s1 s2 returns true if the string s1 is a prefix of the
string s2. Note that the empty string is a prefix of any string, and
that a string is a prefix of itself.
isSubstring
isSubstring s1 s2 returns true if the string s1 is a substring
of the string s2. Note that the empty string is a substring of any
string, and that a string is a substring of itself.
isSuffix
isSuffix s1 s2 returns true if the string s1 is a suffix of the
string s2. Note that the empty string is a suffix of any string, and
that a string is a suffix of itself.
compare
compare (s, t) (or s.compare t) does a lexicographic comparison of the two strings
using the ordering Char.compare on the characters. It returns
LESS, EQUAL, or GREATER, if s is less than, equal to, or
greater than t, respectively.
collate
collate (f, (s, t)) performs lexicographic comparison of the two
strings using the given ordering f on characters.
<
s < t returns true if s is less than t in the string ordering.
<=
s <= t returns true if s is less than or equal to t in the string ordering.
>
s > t returns true if s is greater than t in the string ordering.
>=
s >= t returns true if s is greater than or equal to t in the string
ordering.
=
s = t returns true if s and t are equal.
<>
s <> t returns true if s and t are not equal.