containers
Library
Module
Module type
Parameter
Class
Class type
include module type of struct include StringLabels end
String.get s n
returns the character at index n
in string s
. You can also write s.[n]
instead of String.get s n
.
Raise Invalid_argument
if n
not a valid index in s
.
String.create n
returns a fresh byte sequence of length n
. The sequence is uninitialized and contains arbitrary bytes.
Raise Invalid_argument
if n < 0
or n >
Sys.max_string_length
.
String.make n c
returns a fresh string of length n
, filled with the character c
.
Raise Invalid_argument
if n < 0
or n >
Sys.max_string_length
.
init n f
returns a string of length n
, with character i
initialized to the result of f i
.
Raise Invalid_argument
if n < 0
or n >
Sys.max_string_length
.
String.sub s start len
returns a fresh string of length len
, containing the substring of s
that starts at position start
and has length len
.
Raise Invalid_argument
if start
and len
do not designate a valid substring of s
.
String.fill s start len c
modifies byte sequence s
in place, replacing len
bytes by c
, starting at start
.
Raise Invalid_argument
if start
and len
do not designate a valid substring of s
.
String.concat sep sl
concatenates the list of strings sl
, inserting the separator string sep
between each.
Same as String.iter
, but the function is applied to the index of the element as first argument (counting from 0), and the character itself as second argument.
String.map f s
applies function f
in turn to all the characters of s
and stores the results in a new string that is returned.
String.mapi f s
calls f
with each character of s
and its index (in increasing index order) and stores the results in a new string that is returned.
Return a copy of the argument, without leading and trailing whitespace. The characters regarded as whitespace are: ' '
, '\012'
, '\n'
, '\r'
, and '\t'
. If there is no leading nor trailing whitespace character in the argument, return the original string itself, not a copy.
Return a copy of the argument, with special characters represented by escape sequences, following the lexical conventions of OCaml. If there is no special character in the argument, return the original string itself, not a copy. Its inverse function is Scanf.unescaped.
String.index s c
returns the index of the first occurrence of character c
in string s
.
Raise Not_found
if c
does not occur in s
.
String.index_opt s c
returns the index of the first occurrence of character c
in string s
, or None
if c
does not occur in s
.
String.rindex s c
returns the index of the last occurrence of character c
in string s
.
Raise Not_found
if c
does not occur in s
.
String.rindex_opt s c
returns the index of the last occurrence of character c
in string s
, or None
if c
does not occur in s
.
String.index_from s i c
returns the index of the first occurrence of character c
in string s
after position i
. String.index s c
is equivalent to String.index_from s 0 c
.
Raise Invalid_argument
if i
is not a valid position in s
. Raise Not_found
if c
does not occur in s
after position i
.
String.index_from_opt s i c
returns the index of the first occurrence of character c
in string s
after position i
or None
if c
does not occur in s
after position i
.
String.index_opt s c
is equivalent to String.index_from_opt s 0 c
. Raise Invalid_argument
if i
is not a valid position in s
.
String.rindex_from s i c
returns the index of the last occurrence of character c
in string s
before position i+1
. String.rindex s c
is equivalent to String.rindex_from s (String.length s - 1) c
.
Raise Invalid_argument
if i+1
is not a valid position in s
. Raise Not_found
if c
does not occur in s
before position i+1
.
String.rindex_from_opt s i c
returns the index of the last occurrence of character c
in string s
before position i+1
or None
if c
does not occur in s
before position i+1
.
String.rindex_opt s c
is equivalent to String.rindex_from_opt s (String.length s - 1) c
.
Raise Invalid_argument
if i+1
is not a valid position in s
.
String.contains s c
tests if character c
appears in the string s
.
String.contains_from s start c
tests if character c
appears in s
after position start
. String.contains s c
is equivalent to String.contains_from s 0 c
.
Raise Invalid_argument
if start
is not a valid position in s
.
String.rcontains_from s stop c
tests if character c
appears in s
before position stop+1
.
Raise Invalid_argument
if stop < 0
or stop+1
is not a valid position in s
.
Return a copy of the argument, with all lowercase letters translated to uppercase, including accented letters of the ISO Latin-1 (8859-1) character set.
Return a copy of the argument, with all uppercase letters translated to lowercase, including accented letters of the ISO Latin-1 (8859-1) character set.
Return a copy of the argument, with the first character set to uppercase, using the ISO Latin-1 (8859-1) character set..
Return a copy of the argument, with the first character set to lowercase, using the ISO Latin-1 (8859-1) character set..
val length : t -> int
length s
returns the length (number of characters) of the given string s
.
blit ~src ~src_pos ~dst ~dst_pos ~len
copies len
characters from string src
starting at character indice src_pos
, to the Bytes sequence dst
starting at character indice dst_pos
. Like String.blit
. Compatible with the -safe-string
option.
val fold : f:( 'a -> char -> 'a ) -> init:'a -> t -> 'a
fold ~f ~init s
folds on chars by increasing index. Computes f(… (f (f init s.[0]) s.[1]) …) s.[n-1]
.
val foldi : f:( 'a -> int -> char -> 'a ) -> 'a -> t -> 'a
foldi ~f init s
is just like fold
, but it also passes in the index of each chars as second argument to the folded function f
.
Conversions
to_seq s
returns the Seq.t
of characters contained in the string s
. Renamed from to std_seq
since 3.0.
val to_list : t -> char list
to_list s
returns the list
of characters contained in the string s
.
pp_buf buf s
prints s
to the buffer buf
. Renamed from pp
since 2.0.
val pp : Format.formatter -> t -> unit
pp f s
prints the string s
within quotes to the formatter f
. Renamed from print
since 2.0.
Strings
compare s1 s2
compares the strings s1
and s2
and returns an integer that indicates their relative position in the sort order.
pad ?side ?c n s
ensures that the string s
is at least n
bytes long, and pads it on the side
with c
if it's not the case.
val of_gen : char gen -> string
of_gen gen
converts a gen
of characters to a string.
val of_iter : char iter -> string
of_iter iter
converts an iter
of characters to a string.
val of_seq : char Seq.t -> string
of_seq seq
converts a seq
of characters to a string. Renamed from of_std_seq
since 3.0.
to_array s
returns the array of characters contained in the string s
.
find ?start ~sub s
returns the starting index of the first occurrence of sub
within s
or -1
.
val find_all : ?start:int -> sub:string -> string -> int gen
find_all ?start ~sub s
finds all occurrences of sub
in s
, even overlapping instances and returns them in a generator gen
.
find_all_l ?start ~sub s
finds all occurrences of sub
in s
and returns them in a list.
mem ?start ~sub s
is true
iff sub
is a substring of s
.
rfind ~sub s
finds sub
in string s
from the right, returns its first index or -1
. Should only be used with very small sub
.
replace ?which ~sub ~by s
replaces some occurrences of sub
by by
in s
.
is_sub ~sub ~sub_pos s ~pos ~sub_len
returns true
iff the substring of sub
starting at position sub_pos
and of length sub_len
is a substring of s
starting at position pos
.
chop_prefix ~pre s
removes pre
from s
if pre
really is a prefix of s
, returns None
otherwise.
chop_suffix ~suf s
removes suf
from s
if suf
really is a suffix of s
, returns None
otherwise.
val lines_gen : string -> string gen
lines_gen s
returns a generator gen
of the lines of s
(splits along '\n').
val lines_iter : string -> string iter
lines_iter s
returns the iter
of the lines of s
(splits along '\n').
val lines_seq : string -> string Seq.t
lines_seq s
returns the Seq.t
of the lines of s
(splits along '\n').
val concat_iter : sep:string -> string iter -> string
concat_iter ~sep iter
concatenates all strings of iter
, separated with sep
.
val concat_gen : sep:string -> string gen -> string
concat_gen ~sep gen
concatenates all strings of gen
, separated with sep
.
val concat_seq : sep:string -> string Seq.t -> string
concat_seq ~sep seq
concatenates all strings of seq
, separated with sep
.
val unlines_gen : string gen -> string
unlines_gen gen
concatenates all strings of gen
, separated with '\n'.
val unlines_iter : string iter -> string
unlines_iter iter
concatenates all strings of iter
, separated with '\n'.
val unlines_seq : string Seq.t -> string
unlines_seq seq
concatenates all strings of seq
, separated with '\n'.
set s i c
creates a new string which is a copy of s
, except for index i
, which becomes c
.
iter ~f s
applies function f
on each character of s
. Alias to String.iter
.
filter_map ~f s
calls (f a0) (f a1) … (f an)
where a0 … an
are the characters of s. It returns the string of characters ci
such as f ai = Some ci
(when f
returns None
, the corresponding element of s
is discarded).
filter ~f s
discards characters of s
not satisfying f
.
uniq ~eq s
remove consecutive duplicate characters in s
.
flat_map ?sep ~f s
maps each chars of s
to a string, then concatenates them all.
for_all ~f s
is true
iff all characters of s
satisfy the predicate f
.
exists ~f s
is true
iff some character of s
satisfy the predicate f
.
drop_while ~f s
discards any characters of s
starting from the left, up to the first character c
not satisfying f c
.
rdrop_while ~f s
discards any characters of s
starting from the right, up to the first character c
not satisfying f c
.
ltrim s
trims space on the left (see String.trim
for more details).
rtrim s
trims space on the right (see String.trim
for more details).
Operations on 2 strings
map2 ~f s1 s2
maps pairs of chars.
iter2 ~f s1 s2
iterates on pairs of chars.
iteri2 ~f s1 s2
iterates on pairs of chars with their index.
fold2 ~f ~init s1 s2
folds on pairs of chars.
for_all2 ~f s1 s2
returns true
iff all pairs of chars satisfy the predicate f
.
exists2 ~f s1 s2
returns true
iff a pair of chars satisfy the predicate f
.
Ascii functions
Those functions are deprecated in String
since 4.03, so we provide a stable alias for them even in older versions.
capitalize_ascii s
returns a copy of s
with the first character set to uppercase using the US-ASCII character set. See String
.
uncapitalize_ascii s
returns a copy of s
with the first character set to lowercase using the US-ASCII character set. See String
.
uppercase_ascii s
returns a copy of s
with all lowercase letters translated to uppercase using the US-ASCII character set. See String
.
lowercase_ascii s
returns a copy of s
with all uppercase letters translated to lowercase using the US-ASCII character set. See String
.
equal_caseless s1 s2
compares s1
and s2
without respect to ascii lowercase.
Same as of_hex
but fails harder.
Finding
A relatively efficient algorithm for finding sub-strings.
module Find : sig ... end
Splitting
module Split : sig ... end
split_on_char ~by s
splits the string s
along the given char by
.
split ~by s
splits the string s
along the given string by
. Alias to Split.list_cpy
.
Utils
compare_versions s1 s2
compares version strings s1
and s2
, considering that numbers are above text.
compare_natural s1 s2
is the Natural Sort Order, comparing chunks of digits as natural numbers. https://en.wikipedia.org/wiki/Natural_sort_order
edit_distance ?cutoff s1 s2
is the edition distance between the two strings s1
and s2
. This satisfies the classical distance axioms: it is always positive, symmetric, and satisfies the formula distance s1 s2 + distance s2 s3 >= distance s1 s3
.
Infix operators
module Infix : sig ... end