Additional functions for string manipulations.
init l f
returns the string of length l
with the chars
f 0 , f 1 , f 2 ... f (l-1).
find s x
returns the starting index of the string x
within the string s
or raises Invalid_string
if x
is not a substring of s
.
find s i x
returns the starting index of the string x
within the string s
(starting search from position i
) or
raises Invalid_string
if no such substring exists.
find s x
is equivalent to find_from s 0 x
.
split s sep
splits the string s
between the first
occurrence of sep
.
raises Invalid_string
if the separator is not found.
nsplit s sep
splits the string s
into a list of strings
which are separated by sep
.
nsplit "" _
returns the empty list.
Invalid_string
if sep
is empty string.
Same as concat
slice ?first ?last s
returns a "slice" of the string
which corresponds to the characters s.[first]
,
s.[first+1]
, ..., s[last-1]
. Note that the character at
index last
is not included! If first
is omitted it
defaults to the start of the string, i.e. index 0, and if
last
is omitted is defaults to point just past the end of
s
, i.e. length s
. Thus, slice s
is equivalent to
copy s
.
Negative indexes are interpreted as counting from the end of
the string. For example, slice ~last:-2 s
will return the
string s
, but without the last two characters.
This function never raises any exceptions. If the indexes are out of bounds they are automatically clipped.
Returns the same string but without the first character. does nothing if the string is empty.
Returns the same string but without the last character. does nothing if the string is empty.
Returns the string representation of an int.
Returns the string representation of an float.
Returns a string containing one given character.
Returns the integer represented by the given string or
raises Invalid_string
if the string does not represent an integer.
Returns the float represented by the given string or raises Invalid_string if the string does not represent a float.
ends_with s x
returns true if the string s
is ending with x
.
starts_with s x
return true if s
is starting with x
.
map f s
returns a string where all characters c
in s
have been
replaced by f c
. *
fold_left f a s
is
f (... (f (f a s.[0]) s.[1]) ...) s.[n-1]
fold_right f s b
is
f s.[0] (f s.[1] (... (f s.[n-1] b) ...))
explode s
returns the list of characters in the string s
.
implode cs
returns a string resulting from concatenating
the characters in the list cs
.
Returns the string without the chars if they are at the beginning or at the end of the string. By default chars are " \t\r\n".
exists str sub
returns true if sub
is a substring of str
or
false otherwise.
replace_chars f s
returns a string where all chars c
of s
have been
replaced by the string returned by f c
.
replace ~str ~sub ~by
returns a tuple constisting of a boolean
and a string where the first occurrence of the string sub
within str
has been replaced by the string by
. The boolean
is true if a subtitution has taken place.
Please refer to the Ocaml Manual for documentation of these functions.