Module CCStringLabels
Basic String Utils
type 'a gen
= unit -> 'a option
type 'a sequence
= ('a -> unit) -> unit
type 'a klist
= unit -> [ `Nil | `Cons of 'a * 'a klist ]
Common Signature
module type S = sig ... end
Strings
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 -> f:(int -> char) -> string
val copy : string -> string
val sub : string -> pos:int -> len:int -> string
val fill : bytes -> pos:int -> len:int -> char -> unit
val blit : src:string -> src_pos:int -> dst:bytes -> dst_pos:int -> len:int -> unit
val concat : sep:string -> string list -> string
val iter : f:(char -> unit) -> string -> unit
val iteri : f:(int -> char -> unit) -> string -> unit
val map : f:(char -> char) -> string -> string
val mapi : f:(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 compare : string -> string -> int
val is_empty : string -> bool
is_empty s
returnstrue
iffs
is empty (i.e. its length is 0).- since
- 1.5
val pad : ?side:[ `Left | `Right ] -> ?c:char -> int -> string -> string
pad n str
ensures thatstr
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
Convert a
gen
of characters to a string.
val of_seq : char sequence -> string
Convert a
sequence
of characters to a string.
val of_klist : char klist -> string
Convert a
klist
of characters to a string.
val find : ?start:int -> sub:string -> string -> int
Find
sub
in string, returns its first index or-1
.
val find_all : ?start:int -> sub:string -> string -> int gen
find_all ~sub s
finds all occurrences ofsub
ins
, even overlapping instances.- 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 ~sub s
istrue
iffsub
is a substring ofs
.- since
- 0.12
val rfind : sub:string -> string -> int
Find
sub
in string 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 ~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 -> sub_pos:int -> string -> pos:int -> sub_len:int -> bool
is_sub ~sub i s j ~len
returnstrue
iff the substring ofsub
starting at positioni
and of lengthlen
is a substring ofs
starting at positionj
.
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 a generator of the lines ofs
(splits along '\n').- since
- 0.10
val concat_gen : sep:string -> string gen -> string
concat_gen ~sep g
concatenates all strings ofg
, separated withsep
.- since
- 0.10
val unlines : string list -> string
unlines l
concatenates all strings ofl
, separated with '\n'.- since
- 0.10
val unlines_gen : string gen -> string
unlines_gen g
concatenates all strings ofg
, separated with '\n'.- since
- 0.10
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 filter_map : f:(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 : f:(char -> bool) -> string -> string
filter f s
discards characters not satisfyingf
.- since
- 0.17
val flat_map : ?sep:string -> f:(char -> string) -> string -> string
Map each chars to a string, then concatenates them all.
- parameter sep
optional separator between each generated string.
- since
- 0.12
include S with type S.t := string
val length : t -> int
Return the length (number of characters) of the given string.
val blit : src:t -> src_pos:int -> dst:Stdlib.Bytes.t -> dst_pos:int -> len:int -> unit
Like
String
.blit. Compatible with the-safe-string
option.- raises Invalid_argument
if indices are not valid.
val fold : f:('a -> char -> 'a) -> init:'a -> t -> 'a
Fold on chars by increasing index.
- since
- 0.7
Conversions
val to_list : t -> char list
Return the list of characters contained in the string.
val pp_buf : Stdlib.Buffer.t -> t -> unit
Renamed from
pp
since 2.0.
val pp : Stdlib.Format.formatter -> t -> unit
Print the string within quotes.
Renamed from
print
since 2.0.
val drop_while : f:(char -> bool) -> t -> t
drop_while f s
discards any characters starting from the left, up to the first characterc
not satisfyingf c
.- since
- 2.2
Operations on 2 strings
val map2 : f:(char -> char -> char) -> string -> string -> string
Map pairs of chars.
- raises Invalid_argument
if the strings have not the same length.
- since
- 0.12
val iter2 : f:(char -> char -> unit) -> string -> string -> unit
Iterate on pairs of chars.
- raises Invalid_argument
if the strings have not the same length.
- since
- 0.12
val iteri2 : f:(int -> char -> char -> unit) -> string -> string -> unit
Iterate on pairs of chars with their index.
- raises Invalid_argument
if the strings have not the same length.
- since
- 0.12
val fold2 : f:('a -> char -> char -> 'a) -> init:'a -> string -> string -> 'a
Fold 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 : by:string -> string -> string list
Alias to
Split.list_cpy
.- since
- 1.2
Utils
val compare_versions : string -> string -> int
compare_versions a b
compares version stringsa
andb
, considering that numbers are above text.- since
- 0.13
Slices
A contiguous part of a string
module Sub : sig ... end