Module CCString
Basic String Utils
Documentation for the standard String module
include module type of sig ... end
val concat : string -> string list -> stringval equal : t -> t -> boolval compare : t -> t -> intval contains_from : string -> int -> char -> boolval rcontains_from : string -> int -> char -> boolval contains : string -> char -> boolval sub : string -> int -> int -> stringval split_on_char : char -> string -> string listval map : (char -> char) -> string -> stringval mapi : (int -> char -> char) -> string -> stringval trim : string -> stringval escaped : string -> stringval uppercase_ascii : string -> stringval lowercase_ascii : string -> stringval capitalize_ascii : string -> stringval uncapitalize_ascii : string -> stringval iter : (char -> unit) -> string -> unitval iteri : (int -> char -> unit) -> string -> unitval index_from : string -> int -> char -> intval index_from_opt : string -> int -> char -> int optionval rindex_from : string -> int -> char -> intval rindex_from_opt : string -> int -> char -> int optionval index : string -> char -> intval index_opt : string -> char -> int optionval rindex : string -> char -> intval rindex_opt : string -> char -> int optionval to_seq : t -> char Stdlib.Seq.tval to_seqi : t -> (int * char) Stdlib.Seq.tval of_seq : char Stdlib.Seq.t -> t
val length : t -> intlength sreturns the length (number of characters) of the given strings.
val blit : t -> int -> Stdlib.Bytes.t -> int -> int -> unitblit src src_pos dst dst_pos lencopieslencharacters from stringsrcstarting at character indicesrc_pos, to the Bytes sequencedststarting at character indicedst_pos. LikeString.blit. Compatible with the-safe-stringoption.- raises Invalid_argument
if indices are not valid.
val fold : ('a -> char -> 'a) -> 'a -> t -> 'afold f init sfolds 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 -> 'afoldi f init sis 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 iterto_iter sreturns theiterof characters contained in the strings.- since
- 2.8
val to_seq : t -> char Stdlib.Seq.tto_seq sreturns theSeq.tof characters contained in the strings. Renamed fromto std_seqsince 3.0.- since
- 3.0
val to_list : t -> char listto_list sreturns thelistof characters contained in the strings.
val pp_buf : Stdlib.Buffer.t -> t -> unitpp_buf buf sprintssto the bufferbuf. Renamed fromppsince 2.0.
val pp : Stdlib.Format.formatter -> t -> unitpp f sprints the stringswithin quotes to the formatterf. Renamed fromprintsince 2.0.
val compare : string -> string -> intcompare s1 s2compares the stringss1ands2and returns an integer that indicates their relative position in the sort order.
val pad : ?side:[ `Left | `Right ] -> ?c:char -> int -> string -> stringpad ~side ~c n sensures that the stringsis at leastnbytes long, and pads it on thesidewithcif 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 -> stringof_gen genconverts agenof characters to a string.
val of_iter : char iter -> stringof_iter iterconverts aniterof characters to a string.- since
- 2.8
val of_seq : char Stdlib.Seq.t -> stringof_seq seqconverts aseqof characters to a string. Renamed fromof_std_seqsince 3.0.- since
- 3.0
val to_array : string -> char arrayto_array sreturns the array of characters contained in the strings.
val find : ?start:int -> sub:string -> string -> intfind ~start ~sub sreturns the starting index of the first occurrence ofsubwithinsor-1.- parameter start
starting position in
s.
val find_all : ?start:int -> sub:string -> string -> int genfind_all ~start ~sub sfinds all occurrences ofsubins, 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 listfind_all_l ~sub sfinds all occurrences ofsubinsand returns them in a list.- parameter start
starting position in
s.
- since
- 0.17
val mem : ?start:int -> sub:string -> string -> boolmem ~start ~sub sistrueiffsubis a substring ofs.- since
- 0.12
val rfind : sub:string -> string -> intrfind ~sub sfindssubin stringsfrom 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 -> stringreplace ~which ~sub ~by sreplaces some occurrences ofsubbybyins.- parameter which
decides whether the occurrences to replace are:
`Leftfirst occurrence from the left (beginning).`Rightfirst occurrence from the right (end).`Allall occurrences (default).
- raises Invalid_argument
if
sub = "".
- since
- 0.14
val is_sub : sub:string -> int -> string -> int -> sub_len:int -> boolis_sub ~sub ~sub_pos s ~pos ~sub_lenreturnstrueiff the substring ofsubstarting at positionsub_posand of lengthsub_lenis a substring ofsstarting at positionpos.
val suffix : suf:string -> string -> boolsuffix ~suf sreturnstrueiffsufis a suffix ofs.- since
- 0.7
val chop_prefix : pre:string -> string -> string optionchop_prefix ~pre sremovesprefromsifprereally is a prefix ofs, returnsNoneotherwise.- since
- 0.17
val chop_suffix : suf:string -> string -> string optionchop_suffix ~suf sremovessuffromsifsufreally is a suffix ofs, returnsNoneotherwise.- since
- 0.17
val lines : string -> string listlines sreturns a list of the lines ofs(splits along '\n').- since
- 0.10
val lines_gen : string -> string genlines_gen sreturns thegenof the lines ofs(splits along '\n').- since
- 0.10
val lines_iter : string -> string iterlines_iter sreturns theiterof the lines ofs(splits along '\n').- since
- 3.2
val lines_seq : string -> string Stdlib.Seq.tlines_seq sreturns theSeq.tof the lines ofs(splits along '\n').- since
- 3.2
val concat_gen : sep:string -> string gen -> stringconcat_gen ~sep genconcatenates all strings ofgen, separated withsep.- since
- 0.10
val concat_seq : sep:string -> string Stdlib.Seq.t -> stringconcat_seq ~sep seqconcatenates all strings ofseq, separated withsep.- since
- 3.2
val concat_iter : sep:string -> string iter -> stringconcat_iter ~sep iterconcatenates all strings ofiter, separated withsep.- since
- 3.2
val unlines : string list -> stringunlines lsconcatenates all strings ofls, separated with '\n'.- since
- 0.10
val unlines_gen : string gen -> stringunlines_gen genconcatenates all strings ofgen, separated with '\n'.- since
- 0.10
val unlines_iter : string iter -> stringunlines_iter iterconcatenates all strings ofiter, separated with '\n'.- since
- 3.2
val unlines_seq : string Stdlib.Seq.t -> stringunlines_seq seqconcatenates all strings ofseq, separated with '\n'.- since
- 3.2
val set : string -> int -> char -> stringset s i ccreates a new string which is a copy ofs, except for indexi, which becomesc.- raises Invalid_argument
if
iis an invalid index.
- since
- 0.12
val iter : (char -> unit) -> string -> unititer f sapplies functionfon each character ofs. Alias toString.iter.- since
- 0.12
val filter_map : (char -> char option) -> string -> stringfilter_map f scalls(f a0) (f a1) … (f an)wherea0 … anare the characters of s. It returns the string of characterscisuch asf ai = Some ci(whenfreturnsNone, the corresponding element ofsis discarded).- since
- 0.17
val filter : (char -> bool) -> string -> stringfilter f sdiscards characters ofsnot satisfyingf.- since
- 0.17
val uniq : (char -> char -> bool) -> string -> stringuniq eq sremove consecutive duplicate characters ins.- since
- 3.4
val flat_map : ?sep:string -> (char -> string) -> string -> stringflat_map ~sep f smaps each chars ofsto a string, then concatenates them all.- parameter sep
optional separator between each generated string.
- since
- 0.12
val for_all : (char -> bool) -> string -> boolfor_all f sistrueiff all characters ofssatisfy the predicatef.- since
- 0.12
val exists : (char -> bool) -> string -> boolexists f sistrueiff some character ofssatisfy the predicatef.- since
- 0.12
val drop_while : (char -> bool) -> t -> tdrop_while f sdiscards any characters ofsstarting from the left, up to the first charactercnot satisfyingf c.- since
- 2.2
Operations on 2 strings
val map2 : (char -> char -> char) -> string -> string -> stringmap2 f s1 s2maps pairs of chars.- raises Invalid_argument
if the strings have not the same length.
- since
- 0.12
val iter2 : (char -> char -> unit) -> string -> string -> unititer2 f s1 s2iterates 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 -> unititeri2 f s1 s2iterates 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 -> 'afold2 f init s1 s2folds 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 ... endSplitting
module Split : sig ... endval split_on_char : char -> string -> string listsplit_on_char by ssplits the stringsalong the given charby.- since
- 1.2
val split : by:string -> string -> string listsplit ~by ssplits the stringsalong the given stringby. Alias toSplit.list_cpy.- since
- 1.2
Utils
val compare_versions : string -> string -> intcompare_versions s1 s2compares version stringss1ands2, considering that numbers are above text.- since
- 0.13
val compare_natural : string -> string -> intcompare_natural s1 s2is 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 -> intedit_distance ~cutoff s1 s2is the edition distance between the two stringss1ands2. 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 thancutoffif it's reached in<cutoffiterations. However if the result is< cutoffthen it is accurate.
Infix operators
- since
- 3.0
module Infix : sig ... end