Module CCString
Basic String Utils
Documentation for the standard String module
include module type of sig ... end
val length : string -> int
val get : string -> int -> char
val set : bytes -> int -> char -> unit
val create : int -> bytes
val make : int -> char -> string
val init : int -> (int -> char) -> string
val copy : string -> string
val sub : string -> int -> int -> string
val fill : bytes -> int -> int -> char -> unit
val blit : string -> int -> bytes -> int -> int -> unit
val concat : string -> string list -> string
val iter : (char -> unit) -> string -> unit
val iteri : (int -> char -> unit) -> string -> unit
val map : (char -> char) -> string -> string
val mapi : (int -> char -> char) -> string -> string
val trim : string -> string
val escaped : string -> string
val index : string -> char -> int
val index_opt : string -> char -> int option
val rindex : string -> char -> int
val rindex_opt : string -> char -> int option
val index_from : string -> int -> char -> int
val index_from_opt : string -> int -> char -> int option
val rindex_from : string -> int -> char -> int
val rindex_from_opt : string -> int -> char -> int option
val contains : string -> char -> bool
val contains_from : string -> int -> char -> bool
val rcontains_from : string -> int -> char -> bool
val uppercase : string -> string
val lowercase : string -> string
val capitalize : string -> string
val uncapitalize : string -> string
val uppercase_ascii : string -> string
val lowercase_ascii : string -> string
val capitalize_ascii : string -> string
val uncapitalize_ascii : string -> string
val length : t -> int
length s
returns the length (number of characters) of the given strings
.
val blit : t -> int -> Stdlib.Bytes.t -> int -> int -> unit
blit src src_pos dst dst_pos len
copieslen
characters from stringsrc
starting at character indicesrc_pos
, to the Bytes sequencedst
starting at character indicedst_pos
. LikeString
.blit. Compatible with the-safe-string
option.- raises Invalid_argument
if indices are not valid.
val fold : ('a -> char -> 'a) -> 'a -> t -> 'a
fold f init s
folds on chars by increasing index. Computesf(… (f (f init s.[0]) s.[1]) …) s.[n-1]
.- since
- 0.7
val foldi : ('a -> int -> char -> 'a) -> 'a -> t -> 'a
foldi f init s
is just likefold
, but it also passes in the index of each chars as second argument to the folded functionf
.- since
- 3.3
Conversions
val to_iter : t -> char iter
to_iter s
returns theiter
of characters contained in the strings
.- since
- 2.8
val to_seq : t -> char Stdlib.Seq.t
to_seq s
returns theSeq.t
of characters contained in the strings
. Renamed fromto std_seq
since 3.0.- since
- 3.0
val to_list : t -> char list
to_list s
returns thelist
of characters contained in the strings
.
val pp_buf : Stdlib.Buffer.t -> t -> unit
pp_buf buf s
printss
to the bufferbuf
. Renamed frompp
since 2.0.
val pp : Stdlib.Format.formatter -> t -> unit
pp f s
prints the strings
within quotes to the formatterf
. Renamed fromprint
since 2.0.
val compare : string -> string -> int
compare s1 s2
compares the stringss1
ands2
and returns an integer that indicates their relative position in the sort order.
val pad : ?side:[ `Left | `Right ] -> ?c:char -> int -> string -> string
pad ~side ~c n s
ensures that the strings
is at leastn
bytes long, and pads it on theside
withc
if it's not the case.- parameter side
determines where padding occurs (default:
`Left
).
- parameter c
the char used to pad (default: ' ').
- since
- 0.17
val of_gen : char gen -> string
of_gen gen
converts agen
of characters to a string.
val of_iter : char iter -> string
of_iter iter
converts aniter
of characters to a string.- since
- 2.8
val of_seq : char Stdlib.Seq.t -> string
of_seq seq
converts aseq
of characters to a string. Renamed fromof_std_seq
since 3.0.- since
- 3.0
val to_array : string -> char array
to_array s
returns the array of characters contained in the strings
.
val find : ?start:int -> sub:string -> string -> int
find ~start ~sub s
returns the starting index of the first occurrence ofsub
withins
or-1
.- parameter start
starting position in
s
.
val find_all : ?start:int -> sub:string -> string -> int gen
find_all ~start ~sub s
finds all occurrences ofsub
ins
, even overlapping instances and returns them in a generatorgen
.- parameter start
starting position in
s
.
- since
- 0.17
val find_all_l : ?start:int -> sub:string -> string -> int list
find_all_l ~sub s
finds all occurrences ofsub
ins
and returns them in a list.- parameter start
starting position in
s
.
- since
- 0.17
val mem : ?start:int -> sub:string -> string -> bool
mem ~start ~sub s
istrue
iffsub
is a substring ofs
.- since
- 0.12
val rfind : sub:string -> string -> int
rfind ~sub s
findssub
in strings
from the right, returns its first index or-1
. Should only be used with very smallsub
.- since
- 0.12
val replace : ?which:[ `Left | `Right | `All ] -> sub:string -> by:string -> string -> string
replace ~which ~sub ~by s
replaces some occurrences ofsub
byby
ins
.- parameter which
decides whether the occurrences to replace are:
`Left
first occurrence from the left (beginning).`Right
first occurrence from the right (end).`All
all occurrences (default).
- raises Invalid_argument
if
sub = ""
.
- since
- 0.14
val is_sub : sub:string -> int -> string -> int -> sub_len:int -> bool
is_sub ~sub ~sub_pos s ~pos ~sub_len
returnstrue
iff the substring ofsub
starting at positionsub_pos
and of lengthsub_len
is a substring ofs
starting at positionpos
.
val suffix : suf:string -> string -> bool
suffix ~suf s
returnstrue
iffsuf
is a suffix ofs
.- since
- 0.7
val chop_prefix : pre:string -> string -> string option
chop_prefix ~pre s
removespre
froms
ifpre
really is a prefix ofs
, returnsNone
otherwise.- since
- 0.17
val chop_suffix : suf:string -> string -> string option
chop_suffix ~suf s
removessuf
froms
ifsuf
really is a suffix ofs
, returnsNone
otherwise.- since
- 0.17
val lines : string -> string list
lines s
returns a list of the lines ofs
(splits along '\n').- since
- 0.10
val lines_gen : string -> string gen
lines_gen s
returns thegen
of the lines ofs
(splits along '\n').- since
- 0.10
val lines_iter : string -> string iter
lines_iter s
returns theiter
of the lines ofs
(splits along '\n').- since
- 3.2
val lines_seq : string -> string Stdlib.Seq.t
lines_seq s
returns theSeq.t
of the lines ofs
(splits along '\n').- since
- 3.2
val concat_gen : sep:string -> string gen -> string
concat_gen ~sep gen
concatenates all strings ofgen
, separated withsep
.- since
- 0.10
val concat_seq : sep:string -> string Stdlib.Seq.t -> string
concat_seq ~sep seq
concatenates all strings ofseq
, separated withsep
.- since
- 3.2
val concat_iter : sep:string -> string iter -> string
concat_iter ~sep iter
concatenates all strings ofiter
, separated withsep
.- since
- 3.2
val unlines : string list -> string
unlines ls
concatenates all strings ofls
, separated with '\n'.- since
- 0.10
val unlines_gen : string gen -> string
unlines_gen gen
concatenates all strings ofgen
, separated with '\n'.- since
- 0.10
val unlines_iter : string iter -> string
unlines_iter iter
concatenates all strings ofiter
, separated with '\n'.- since
- 3.2
val unlines_seq : string Stdlib.Seq.t -> string
unlines_seq seq
concatenates all strings ofseq
, separated with '\n'.- since
- 3.2
val set : string -> int -> char -> string
set s i c
creates a new string which is a copy ofs
, except for indexi
, which becomesc
.- raises Invalid_argument
if
i
is an invalid index.
- since
- 0.12
val iter : (char -> unit) -> string -> unit
iter f s
applies functionf
on each character ofs
. Alias toString
.iter.- since
- 0.12
val filter_map : (char -> char option) -> string -> string
filter_map f s
calls(f a0) (f a1) … (f an)
wherea0 … an
are the characters of s. It returns the string of charactersci
such asf ai = Some ci
(whenf
returnsNone
, the corresponding element ofs
is discarded).- since
- 0.17
val filter : (char -> bool) -> string -> string
filter f s
discards characters ofs
not satisfyingf
.- since
- 0.17
val uniq : (char -> char -> bool) -> string -> string
uniq eq s
remove consecutive duplicate characters ins
.- since
- 3.4
val flat_map : ?sep:string -> (char -> string) -> string -> string
flat_map ~sep f s
maps each chars ofs
to a string, then concatenates them all.- parameter sep
optional separator between each generated string.
- since
- 0.12
val for_all : (char -> bool) -> string -> bool
for_all f s
istrue
iff all characters ofs
satisfy the predicatef
.- since
- 0.12
val exists : (char -> bool) -> string -> bool
exists f s
istrue
iff some character ofs
satisfy the predicatef
.- since
- 0.12
val drop_while : (char -> bool) -> t -> t
drop_while f s
discards any characters ofs
starting from the left, up to the first characterc
not satisfyingf c
.- since
- 2.2
Operations on 2 strings
val map2 : (char -> char -> char) -> string -> string -> string
map2 f s1 s2
maps pairs of chars.- raises Invalid_argument
if the strings have not the same length.
- since
- 0.12
val iter2 : (char -> char -> unit) -> string -> string -> unit
iter2 f s1 s2
iterates on pairs of chars.- raises Invalid_argument
if the strings have not the same length.
- since
- 0.12
val iteri2 : (int -> char -> char -> unit) -> string -> string -> unit
iteri2 f s1 s2
iterates on pairs of chars with their index.- raises Invalid_argument
if the strings have not the same length.
- since
- 0.12
val fold2 : ('a -> char -> char -> 'a) -> 'a -> string -> string -> 'a
fold2 f init s1 s2
folds on pairs of chars.- raises Invalid_argument
if the strings have not the same length.
- since
- 0.12
Ascii functions
Those functions are deprecated in String
since 4.03, so we provide a stable alias for them even in older versions.
Finding
A relatively efficient algorithm for finding sub-strings.
- since
- 1.0
module Find : sig ... end
Splitting
module Split : sig ... end
val split_on_char : char -> string -> string list
split_on_char by s
splits the strings
along the given charby
.- since
- 1.2
val split : by:string -> string -> string list
split ~by s
splits the strings
along the given stringby
. Alias toSplit.list_cpy
.- since
- 1.2
Utils
val compare_versions : string -> string -> int
compare_versions s1 s2
compares version stringss1
ands2
, considering that numbers are above text.- since
- 0.13
val compare_natural : string -> string -> int
compare_natural s1 s2
is the Natural Sort Order, comparing chunks of digits as natural numbers. https://en.wikipedia.org/wiki/Natural_sort_order- since
- 1.3
val edit_distance : ?cutoff:int -> string -> string -> int
edit_distance ~cutoff s1 s2
is the edition distance between the two stringss1
ands2
. This satisfies the classical distance axioms: it is always positive, symmetric, and satisfies the formuladistance s1 s2 + distance s2 s3 >= distance s1 s3
.- parameter cutoff
if provided, it's a cap on the number of iterations. (since 3.0). This is useful if you just want to check whether the edit distance is less or equal than 2 without (use
edit_distance s1 s2 ~cutoff:3 <= 2
). note that contrary to what was previously documented here, the result can still be higher thancutoff
if it's reached in<cutoff
iterations. However if the result is< cutoff
then it is accurate.
Infix operators
- since
- 3.0
module Infix : sig ... end