Module CCStringLabels Source Basic String Utils (Labeled version of CCString )
Source type 'a iter = ('a -> unit) -> unit Source type 'a gen = unit -> 'a option StringsSource val make : int -> char -> stringmake n c is a string of length n with each index holding the character c.
Source val init : int -> f :(int -> char) -> stringinit n ~f is a string of length n with index i holding the character f i (called in increasing index order).
Source val get : string -> int -> charget s i is the character at index i in s. This is the same as writing s.[i].
Source val of_bytes : bytes -> stringReturn a new string that contains the same bytes as the given byte sequence.
Source val to_bytes : string -> bytesReturn a new byte sequence that contains the same bytes as the given string.
ConcatenatingNote. The Stdlib.(^) binary operator concatenates two strings.
Source val concat : sep :string -> string list -> stringconcat ~sep ss concatenates the list of strings ss, inserting the separator string sep between each.
Source val cat : string -> string -> stringcat s1 s2 concatenates s1 and s2 (s1 ^ s2).
Predicates and comparisonsSource val starts_with : prefix :string -> string -> boolstarts_with ~prefix s is true if and only if s starts with prefix.
Source val ends_with : suffix :string -> string -> boolends_with ~suffix s is true if and only if s ends with suffix.
Source val contains_from : string -> int -> char -> boolcontains_from s start c is true if and only if c appears in s after position start.
Source val rcontains_from : string -> int -> char -> boolrcontains_from s stop c is true if and only if c appears in s before position stop+1.
Source val contains : string -> char -> boolSource val sub : string -> pos :int -> len :int -> stringsub s ~pos ~len is a string of length len, containing the substring of s that starts at position pos and has length len.
Source val map : f :(char -> char) -> string -> stringmap f s is the string resulting from applying f to all the characters of s in increasing order.
Source val mapi : f :(int -> char -> char) -> string -> stringmapi ~f s is like map but the index of the character is also passed to f.
Source val fold_left : f :('acc -> char -> 'acc ) -> init :'acc -> string -> 'acc fold_left f x s computes f (... (f (f x s.[0]) s.[1]) ...) s.[n-1], where n is the length of the string s.
Source val fold_right : f :(char -> 'acc -> 'acc ) -> string -> init :'acc -> 'acc fold_right f s x computes f s.[0] (f s.[1] ( ... (f s.[n-1] x) ...)), where n is the length of the string s.
Source val trim : string -> stringtrim s is s without leading and trailing whitespace. Whitespace characters are: ' ', '\x0C' (form feed), '\n', '\r', and '\t'.
Source val escaped : string -> stringescaped s is s with special characters represented by escape sequences, following the lexical conventions of OCaml.
All characters outside the US-ASCII printable range [0x20;0x7E] are escaped, as well as backslash (0x2F) and double-quote (0x22).
The function Scanf.unescaped is a left inverse of escaped, i.e. Scanf.unescaped (escaped s) = s for any string s (unless escaped s fails).
TraversingSource val iteri : f :(int -> char -> unit) -> string -> unititeri is like iter , but the function is also given the corresponding character index.
SearchingSource val index_from : string -> int -> char -> intindex_from s i c is the index of the first occurrence of c in s after position i.
Source val index_from_opt : string -> int -> char -> int option index_from_opt s i c is the index of the first occurrence of c in s after position i (if any).
Source val rindex_from : string -> int -> char -> intrindex_from s i c is the index of the last occurrence of c in s before position i+1.
Source val rindex_from_opt : string -> int -> char -> int option rindex_from_opt s i c is the index of the last occurrence of c in s before position i+1 (if any).
Source val index : string -> char -> intSource val index_opt : string -> char -> int option Source val rindex : string -> char -> intSource val rindex_opt : string -> char -> int option Strings and Sequencesto_seqi s is like to_seq but also tuples the corresponding index.
UTF decoding and validations UTF-8get_utf_8_uchar b i decodes an UTF-8 character at index i in b.
Source val is_valid_utf_8 : t -> boolis_valid_utf_8 b is true if and only if b contains valid UTF-8 data.
UTF-16BEget_utf_16be_uchar b i decodes an UTF-16BE character at index i in b.
Source val is_valid_utf_16be : t -> boolis_valid_utf_16be b is true if and only if b contains valid UTF-16BE data.
UTF-16LEget_utf_16le_uchar b i decodes an UTF-16LE character at index i in b.
Source val is_valid_utf_16le : t -> boolis_valid_utf_16le b is true if and only if b contains valid UTF-16LE data.
Binary decoding of integersThe functions in this section binary decode integers from strings.
All following functions raise Invalid_argument if the characters needed at index i to decode the integer are not available.
Little-endian (resp. big-endian) encoding means that least (resp. most) significant bytes are stored first. Big-endian is also known as network byte order. Native-endian encoding is either little-endian or big-endian depending on Sys.big_endian .
32-bit and 64-bit integers are represented by the int32 and int64 types, which can be interpreted either as signed or unsigned numbers.
8-bit and 16-bit integers are represented by the int type, which has more bits than the binary encoding. These extra bits are sign-extended (or zero-extended) for functions which decode 8-bit or 16-bit integers and represented them with int values.
Source val get_uint8 : string -> int -> intget_uint8 b i is b's unsigned 8-bit integer starting at character index i.
Source val get_int8 : string -> int -> intget_int8 b i is b's signed 8-bit integer starting at character index i.
Source val get_uint16_ne : string -> int -> intget_uint16_ne b i is b's native-endian unsigned 16-bit integer starting at character index i.
Source val get_uint16_be : string -> int -> intget_uint16_be b i is b's big-endian unsigned 16-bit integer starting at character index i.
Source val get_uint16_le : string -> int -> intget_uint16_le b i is b's little-endian unsigned 16-bit integer starting at character index i.
Source val get_int16_ne : string -> int -> intget_int16_ne b i is b's native-endian signed 16-bit integer starting at character index i.
Source val get_int16_be : string -> int -> intget_int16_be b i is b's big-endian signed 16-bit integer starting at character index i.
Source val get_int16_le : string -> int -> intget_int16_le b i is b's little-endian signed 16-bit integer starting at character index i.
Source val get_int32_ne : string -> int -> int32get_int32_ne b i is b's native-endian 32-bit integer starting at character index i.
Source val seeded_hash : int -> t -> intSource val get_int32_be : string -> int -> int32get_int32_be b i is b's big-endian 32-bit integer starting at character index i.
Source val get_int32_le : string -> int -> int32get_int32_le b i is b's little-endian 32-bit integer starting at character index i.
Source val get_int64_ne : string -> int -> int64get_int64_ne b i is b's native-endian 64-bit integer starting at character index i.
Source val get_int64_be : string -> int -> int64get_int64_be b i is b's big-endian 64-bit integer starting at character index i.
Source val get_int64_le : string -> int -> int64get_int64_le b i is b's little-endian 64-bit integer starting at character index i.
length s returns the length (number of characters) of the given string s.
Source val blit :
src :t ->
src_pos :int ->
dst :Bytes.t ->
dst_pos :int ->
len :int ->
unitblit ~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.
Source 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].
Source 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.
Conversionsto_gen s returns the gen of characters contained in the string s.
to_iter s returns the iter of characters contained in the string s.
to_seq s returns the Seq.t of characters contained in the string s. Renamed from to std_seq since 3.0.
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.
pp f s prints the string s within quotes to the formatter f. Renamed from print since 2.0.
StringsSource val equal : string -> string -> boolequal s1 s2 returns true iff the strings s1 and s2 are equal.
Source val compare : string -> string -> intcompare s1 s2 compares the strings s1 and s2 and returns an integer that indicates their relative position in the sort order.
Source val is_empty : string -> boolis_empty s returns true iff s is empty (i.e. its length is 0).
Source val hash : string -> inthash s returns the hash value of s.
Source val rev : string -> stringrev s returns the reverse of s.
Source val pad : ?side :[ `Left | `Right ] -> ?c :char -> int -> string -> stringpad ?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.
Source val of_char : char -> stringof_gen gen converts a gen of characters to a string.
of_iter iter converts an iter of characters to a string.
of_seq seq converts a seq of characters to a string. Renamed from of_std_seq since 3.0.
Source val of_list : char list -> stringof_list lc converts a list of characters lc to a string.
Source val of_array : char array -> stringof_array ac converts an array of characters ac to a string.
Source val to_array : string -> char array to_array s returns the array of characters contained in the string s.
Source val find : ?start :int -> sub :string -> string -> intfind ?start ~sub s returns the starting index of the first occurrence of sub within s or -1.
Source 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.
Source val find_all_l : ?start :int -> sub :string -> string -> int list find_all_l ?start ~sub s finds all occurrences of sub in s and returns them in a list.
Source val mem : ?start :int -> sub :string -> string -> boolmem ?start ~sub s is true iff sub is a substring of s.
Source val rfind : sub :string -> string -> intrfind ~sub s finds sub in string s from the right, returns its first index or -1. Should only be used with very small sub.
Source val replace :
?which :[ `Left | `Right | `All ] ->
sub :string ->
by :string ->
string ->
stringreplace ?which ~sub ~by s replaces some occurrences of sub by by in s.
Source val is_sub :
sub :string ->
sub_pos :int ->
string ->
pos :int ->
sub_len :int ->
boolis_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.
Source val repeat : string -> int -> stringrepeat s n creates a string by repeating the string s n times.
Source val prefix : pre :string -> string -> boolprefix ~pre s returns true iff pre is a prefix of s.
Source val suffix : suf :string -> string -> boolsuffix ~suf s returns true iff suf is a suffix of s.
Source val chop_prefix : pre :string -> string -> string option chop_prefix ~pre s removes pre from s if pre really is a prefix of s, returns None otherwise.
Source val chop_suffix : suf :string -> string -> string option chop_suffix ~suf s removes suf from s if suf really is a suffix of s, returns None otherwise.
Source val take : int -> string -> stringtake n s keeps only the n first chars of s.
Source val drop : int -> string -> stringdrop n s removes the n first chars of s.
Source val take_drop : int -> string -> string * stringtake_drop n s is take n s, drop n s.
Source val lines : string -> string list lines s returns a list of the lines of s (splits along '\n').
lines_gen s returns a generator gen of the lines of s (splits along '\n').
lines_iter s returns the iter of the lines of s (splits along '\n').
lines_seq s returns the Seq.t of the lines of s (splits along '\n').
Source val concat_iter : sep :string -> string iter -> stringconcat_iter ~sep iter concatenates all strings of iter, separated with sep.
Source val concat_gen : sep :string -> string gen -> stringconcat_gen ~sep gen concatenates all strings of gen, separated with sep.
Source val concat_seq : sep :string -> string Seq.t -> stringconcat_seq ~sep seq concatenates all strings of seq, separated with sep.
Source val unlines : string list -> stringunlines ls concatenates all strings of ls, separated with '\n'.
unlines_gen gen concatenates all strings of gen, separated with '\n'.
unlines_iter iter concatenates all strings of iter, separated with '\n'.
unlines_seq seq concatenates all strings of seq, separated with '\n'.
Source val set : string -> int -> char -> stringset s i c creates a new string which is a copy of s, except for index i, which becomes c.
Source val iter : f :(char -> unit) -> string -> unititer ~f s applies function f on each character of s. Alias to String.iter .
Source val filter_map : f :(char -> char option ) -> string -> stringfilter_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).
Source val filter : f :(char -> bool) -> string -> stringfilter ~f s discards characters of s not satisfying f.
Source val uniq : eq :(char -> char -> bool) -> string -> stringuniq ~eq s remove consecutive duplicate characters in s.
Source val flat_map : ?sep :string -> f :(char -> string) -> string -> stringflat_map ?sep ~f s maps each chars of s to a string, then concatenates them all.
Source val for_all : f :(char -> bool) -> string -> boolfor_all ~f s is true iff all characters of s satisfy the predicate f.
Source val exists : f :(char -> bool) -> string -> boolexists ~f s is true iff some character of s satisfy the predicate f.
Source val drop_while : f :(char -> bool) -> t -> t drop_while ~f s discards any characters of s starting from the left, up to the first character c not satisfying f c.
Source val rdrop_while : f :(char -> bool) -> t -> t 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 stringsSource val map2 : f :(char -> char -> char) -> string -> string -> stringmap2 ~f s1 s2 maps pairs of chars.
Source val iter2 : f :(char -> char -> unit) -> string -> string -> unititer2 ~f s1 s2 iterates on pairs of chars.
Source val iteri2 : f :(int -> char -> char -> unit) -> string -> string -> unititeri2 ~f s1 s2 iterates on pairs of chars with their index.
Source val fold2 : f :('a -> char -> char -> 'a ) -> init :'a -> string -> string -> 'a fold2 ~f ~init s1 s2 folds on pairs of chars.
Source val for_all2 : f :(char -> char -> bool) -> string -> string -> boolfor_all2 ~f s1 s2 returns true iff all pairs of chars satisfy the predicate f.
Source val exists2 : f :(char -> char -> bool) -> string -> string -> boolexists2 ~f s1 s2 returns true iff a pair of chars satisfy the predicate f.
Ascii functionsThose functions are deprecated in String since 4.03, so we provide a stable alias for them even in older versions.
Source val capitalize_ascii : string -> stringcapitalize_ascii s returns a copy of s with the first character set to uppercase using the US-ASCII character set. See String .
Source val uncapitalize_ascii : string -> stringuncapitalize_ascii s returns a copy of s with the first character set to lowercase using the US-ASCII character set. See String .
Source val uppercase_ascii : string -> stringuppercase_ascii s returns a copy of s with all lowercase letters translated to uppercase using the US-ASCII character set. See String .
Source val lowercase_ascii : string -> stringlowercase_ascii s returns a copy of s with all uppercase letters translated to lowercase using the US-ASCII character set. See String .
Source val equal_caseless : string -> string -> boolequal_caseless s1 s2 compares s1 and s2 without respect to ascii lowercase.
Source val to_hex : string -> stringConvert a string with arbitrary content into a hexadecimal string.
Source val of_hex : string -> string option Convert a string in hex into a string with arbitrary content.
Source val of_hex_exn : string -> stringSame as of_hex but fails harder.
FindingA relatively efficient algorithm for finding sub-strings.
SplittingSource val split_on_char : by :char -> string -> string list split_on_char ~by s splits the string s along the given char by.
Source val split : by :string -> string -> string list split ~by s splits the string s along the given string by. Alias to Split.list_cpy .
UtilsSource val compare_versions : string -> string -> intcompare_versions s1 s2 compares version strings s1 and s2, considering that numbers are above text.
Source val compare_natural : string -> string -> intcompare_natural s1 s2 is the Natural Sort Order, comparing chunks of digits as natural numbers. https://en.wikipedia.org/wiki/Natural_sort_order
Source val edit_distance : ?cutoff :int -> string -> string -> intedit_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 operatorsinclude module type of Infix