StdLabels.String(3)
โจ StdLabels.String โจ โ OCaml library
okay so bestie, this is the StdLabels.String page, and it's literally the labeled-string side of the OCaml library. no description in NAME, but the vibes are immaculate and the module is right here ๐
๐ NAME ๐
StdLabels.String โ no description. iconic, honestly ๐
๐งฉ Module ๐งฉ
Module StdLabels.String
๐ Documentation ๐
๐งฉ Module String : (module StringLabels) ๐งฉ
๐ธ Strings ๐ธ
type t = stringโ the type for strings. yes bestie, it's juststringunder all that label glam โจval make : int -> char -> stringโmake n cis a string of lengthnwith each index holding the characterc.
Raises Invalid_argument if n < 0 or n > Sys.max_string_length.
val init : int -> f:(int -> char) -> stringโinit n ~fis a string of lengthnwith indexiholding the characterf i(called in increasing index order).
Since 4.02
Raises Invalid_argument if n < 0 or n > Sys.max_string_length.
val empty : stringโ the empty string. short, sweet, literally nothing in it ๐ซถ
Since 4.13
val length : string -> intโlength sis the length (number of bytes/characters) ofs.val get : string -> int -> charโget s iis the character at indexiins. this is the same as writings.[i].
Raises Invalid_argument if i not an index of s.
val of_char : char -> stringโof_char ciscas a string.
Since 5.5
val of_bytes : bytes -> stringโ return a new string that contains the same bytes as the given byte sequence.
Since 4.13
val to_bytes : string -> bytesโ return a new byte sequence that contains the same bytes as the given string.
Since 4.13
val blit : src:string -> src_pos:int -> dst:bytes -> dst_pos:int -> len:int -> unitโ same asBytes.blit_stringwhich should be preferred. ok so use that one first, periodt ๐
๐ Concatenating ๐
Note. The (^) binary operator concatenates two strings.
val concat : sep:string -> string list -> stringโconcat ~sep ssconcatenates the list of stringsss, inserting the separator stringsepbetween each.
Raises Invalid_argument if the result is longer than Sys.max_string_length bytes.
val cat : string -> string -> stringโcat s1 s2concatenatess1ands2(s1 ^ s2).
Since 4.13
Raises Invalid_argument if the result is longer than Sys.max_string_length bytes.
๐ต๏ธ Predicates and comparisons ๐ต๏ธ
val equal : t -> t -> boolโequal s0 s1istrueif and only ifs0ands1are character-wise equal.
Since 4.05
val compare : t -> t -> intโcompare s0 s1sortss0ands1in lexicographical order.comparebehaves likecompareon strings but may be more efficient.val is_empty : string -> boolโis_empty sistrueif and only ifsis an empty string.
Since 5.5
val starts_with : prefix:string -> string -> boolโstarts_with ~prefix sistrueif and only ifsstarts withprefix.
Since 4.13
val ends_with : suffix:string -> string -> boolโends_with ~suffix sistrueif and only ifsends withsuffix.
Since 4.13
val includes : affix:string -> string -> boolโincludes ~affix sistrueif and only ifaffixoccurs ins.
Note. To test the sameaffixstring multiple times, partially applying the~affixargument and using the resulting function repeatedly is more efficient. bestie, pre-bind it and slay โจ
Since 5.5
val contains_from : string -> int -> char -> boolโcontains_from s start cistrueif and only ifcappears insafter positionstart.
Raises Invalid_argument if start is not a valid position in s.
val rcontains_from : string -> int -> char -> boolโrcontains_from s stop cistrueif and only ifcappears insbefore positionstop+1.
Raises Invalid_argument if stop < 0 or stop+1 is not a valid position in s.
val contains : string -> char -> boolโcontains s cisString.contains_from s 0 c.
โ๏ธ Extracting substrings โ๏ธ
val sub : string -> pos:int -> len:int -> stringโsub s ~pos ~lenis a string of lengthlen, containing the substring ofsthat starts at positionposand has lengthlen.
Raises Invalid_argument if pos and len do not designate a valid substring of s.
๐ฐ Splitting strings ๐ฐ
๐ Splitting with magnitudes ๐
val take_first : int -> string -> stringโtake_first n sare the firstnbytes ofs. this issifn >= length sand""ifn <= 0.
Since 5.5
val take_last : int -> string -> stringโtake_last n sare the lastnbytes ofs. this issifn >= length sand""ifn <= 0.
Since 5.5
val drop_first : int -> string -> stringโdrop_first n sisswithout the firstnbytes ofs. this is""ifn >= length sandsifn <= 0.
Since 5.5
val drop_last : int -> string -> stringโdrop_last n sisswithout the lastnbytes ofs. this is""ifn >= length sandsifn <= 0.
Since 5.5
val cut_first : int -> string -> string * stringโcut_first n vis(take_first n v, drop_first n v).
Since 5.5
val cut_last : int -> string -> string * stringโcut_last n vis(drop_last n v, take_last n v).
Since 5.5
๐ฏ Splitting with predicates ๐ฏ
val take_first_while : (char -> bool) -> string -> stringโtake_first_while p sis the first consecutive bytes ofssatisfying the predicatep.
Since 5.5
val take_last_while : (char -> bool) -> string -> stringโtake_last_while p sis the last consecutive bytes ofssatisfying the predicatep.
Since 5.5
val drop_first_while : (char -> bool) -> string -> stringโdrop_first_while p sisswithout the first consecutive bytes ofssatisfying the predicatep.
Since 5.5
val drop_last_while : (char -> bool) -> string -> stringโdrop_last_while p sisswithout the last consecutive bytes ofssatisfying the predicatep.
Since 5.5
val cut_first_while : (char -> bool) -> string -> string * stringโcut_first_while p sis(take_first_while p s, drop_first_while p s).
Since 5.5
val cut_last_while : (char -> bool) -> string -> string * stringโcut_last_while p sis(drop_last_while p s, take_last_while p s).
Since 5.5
๐ช Splitting with separators ๐ช
Note. To split the samesepstring multiple times, partially applying the~separgument of these functions and using the resulting function repeatedly is more efficient. pre-bind and go off ๐ฅ
val split_first : sep:string -> string -> (string * string) optionโsplit_first ~sep sis the pairSome (left, right)made of the two (possibly empty) substrings ofsthat are delimited by the first match of the separatorsepinsorNoneifsepcan't be found. search forsepstarts at position0and usesStringLabels.find_first.
if sep is "", this is Some ("", s).
the invariant concat sep [left; right] = s holds.
Since 5.5
val split_last : sep:string -> string -> (string * string) optionโsplit_last ~sep sis the pairSome (left, right)made of the two (possibly empty) substrings ofsthat are delimited by the last match of the separatorsepinsorNoneifsepcan't be found. search forsepstarts at positionlength sand usesStringLabels.find_last.
if sep is "", this is Some (s, "").
the invariant concat sep [left; right] = s holds.
Since 5.5
val split_all : sep:string -> ?drop:(string -> bool) -> string -> string listโsplit_all ~sep sis the list of all substrings ofsthat are delimited by non-overlapping matches of the separatorsepor the list[s]ifsepcan't be found. search forsepstarts at position0in increasing indexing order and usesStringLabels.find_all.
substrings sub for which drop sub is true are not included in the result. drop defaults to Fun.const false.
if sep is "", this is [""; c0; ...; cn; ""] with ci the string of_char s.[i].
the invariant concat sep (split_all ~sep s) = s holds.
Since 5.5
val rsplit_all : sep:string -> ?drop:(string -> bool) -> string -> string listโrsplit_all ~sep sis the list of all substrings ofsthat are delimited by non-overlapping matches of the separatorsepor[s]ifsepcan't be found. search forsepstarts at positionlength sin decreasing indexing order and usesStringLabels.rfind_all.
substrings sub for which drop sub is true are not included in the result. drop defaults to Fun.const false.
if sep is "", this is [""; c0; ...; cn; ""] with ci the string of_char s.[i].
the invariant concat sep (rsplit_all ~sep s) = s holds.
Since 5.5
val split_on_char : sep:char -> string -> string listโsplit_on_char ~sep sis the list of all (possibly empty) substrings ofsthat are delimited by the charactersep. ifsis empty, the result is the singleton list[""].
the function's result is specified by the following invariants:
-The list is not empty. -Concatenating its elements using sep as a separator returns a string equal to the input ( concat (make 1 sep) (split_on_char sep s) = s ). -No string in the result contains the sep character.
Since 4.05
๐ฆ Transforming ๐ฆ
val map : f:(char -> char) -> string -> stringโmap f sis the string resulting from applyingfto all the characters ofsin increasing order.
Since 4.00
val mapi : f:(int -> char -> char) -> string -> stringโmapi ~f sis likeStringLabels.mapbut the index of the character is also passed tof.
Since 4.02
val fold_left : f:('acc -> char -> 'acc) -> init:'acc -> string -> 'accโfold_left f x scomputesf (... (f (f x s.[0]) s.[1]) ...) s.[n-1], wherenis the length of the strings.
Since 4.13
val fold_right : f:(char -> 'acc -> 'acc) -> string -> init:'acc -> 'accโfold_right f s xcomputesf s.[0] (f s.[1] ( ... (f s.[n-1] x) ...)), wherenis the length of the strings.
Since 4.13
val for_all : f:(char -> bool) -> string -> boolโfor_all p schecks if all characters inssatisfy the predicatep.
Since 4.13
val exists : f:(char -> bool) -> string -> boolโexists p schecks if at least one character ofssatisfies the predicatep.
Since 4.13
val trim : string -> stringโtrim sisswithout leading and trailing whitespace. whitespace characters are:' ','\x0C'(form feed),'\n','\r', and'\t'.
Since 4.00
val escaped : string -> stringโescaped sisswith 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 (0x5C) 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).
Raises Invalid_argument if the result is longer than Sys.max_string_length bytes.
val uppercase_ascii : string -> stringโuppercase_ascii sisswith all lowercase letters translated to uppercase, using the US-ASCII character set.
Since 4.05
val lowercase_ascii : string -> stringโlowercase_ascii sisswith all uppercase letters translated to lowercase, using the US-ASCII character set.
Since 4.05
val capitalize_ascii : string -> stringval capitalize_ascii : string -> stringโcapitalize_ascii sissbut with the first character bumped up to uppercase, using the US-ASCII character set ๐
Since 4.05
val uncapitalize_ascii : string -> stringโuncapitalize_ascii sisswith the first character knocked down to lowercase, again using the US-ASCII character set โจ
Since 4.05
๐ Traversing
val iter : f:(char -> unit) -> string -> unitโiter ~f shits functionfwith every single character ofs, one after another. It's literally equivalent to:
f s.[0]; f s.[1]; ...; f s.[length s - 1]; ()
val iteri : f:(int -> char -> unit) -> string -> unitโiteriis giving the exact same energy asStringLabels.iter, except the function also gets handed the matching character index ๐ซถ
Since 4.00
๐ Finding indices
val find_first_index : (char -> bool) -> ?start:int -> string -> int optionโfind_first_index p ~start sgives you the index of the first character ofsthat satisfies predicatep, at or after the index or positionstart(which defaults to0). Ifstartislength s, the result is alwaysNone. Iconic.
Since 5.5
Raises Invalid_argument if start is not a valid position of s. ๐
val find_last_index : (char -> bool) -> ?start:int -> string -> int optionโfind_last_index p ~start sis the index of the last character ofssatisfying predicatep, at or before the index or positionstart(defaults tolength s).
Since 5.5
Raises Invalid_argument if start is not a valid position of s. ๐
val index_from : string -> int -> char -> intโindex_from s i cis the index of the first occurrence ofcinsafter positioni.
Raises Not_found if c does not occur in s after position i. ๐ญ
Raises Invalid_argument if i is not a valid position in s. ๐
val index_from_opt : string -> int -> char -> int optionโindex_from_opt s i cis the index of the first occurrence ofcinsafter positioni(if any). the way it just returns an option instead of throwing, we love a calm queen ๐
Since 4.05
Raises Invalid_argument if i is not a valid position in s. ๐
val rindex_from : string -> int -> char -> intโrindex_from s i cis the index of the last occurrence ofcinsbefore positioni+1.
Raises Not_found if c does not occur in s before position i+1. ๐ญ
Raises Invalid_argument if i+1 is not a valid position in s. ๐
val rindex_from_opt : string -> int -> char -> int optionโrindex_from_opt s i cis the index of the last occurrence ofcinsbefore positioni+1(if any).
Since 4.05
Raises Invalid_argument if i+1 is not a valid position in s. ๐
val index : string -> char -> intโindex s cis literally justString.index_from s 0 c. No cap.val index_opt : string -> char -> int optionโindex_opt s cisString.index_from_opt s 0 c.
Since 4.05
val rindex : string -> char -> intโrindex s cisString.rindex_from s (length s - 1) c.val rindex_opt : string -> char -> int optionโrindex_opt s cisString.rindex_from_opt s (length s - 1) c.
Since 4.05
๐งต Finding substrings
Note. If you're trying to find the same sub string multiple times, partially applying the ~sub argument of these functions and reusing the resulting function is more efficient. lowkey a glow-up for your perf โจ
val find_first : sub:string -> ?start:int -> string -> int optionโfind_first ~sub ~start sis the starting position of the first occurrence ofsubinsat or after the index or positionstart(defaults to0). Ifsubis""the result isSome start. The result of the function is always a valid index ofsโ except whensubis""andstartislength s. ๐
Since 5.5
Raises Invalid_argument if start is not a valid position of s. ๐
val find_last : sub:string -> ?start:int -> string -> int optionโfind_last ~sub ~start sis the starting position of the last occurrence ofsubinsat or before the index or positionstart(defaults toString.length s). Ifsubis""the result isSome start. Result is always a valid index ofs, except whensubis""andstartislength s. ๐ฆ
Since 5.5
Raises Invalid_argument if start is not a valid position of s. ๐
val find_all : sub:string -> f:(int -> 'acc -> 'acc) -> ?start:int -> string -> 'acc -> 'accโfind_all ~sub f ~start s acc, starting withacc, foldsfin increasing index order over all the non-overlapping starting positions ofsubinsat or after the index or positionstart(defaults to0). The result isaccifsubcouldn't be found insat all. Ifsubis"",fgets invoked on all positions ofsat or afterstart.
Since 5.5
Raises Invalid_argument if start is not a valid position of s. ๐
val rfind_all : sub:string -> f:(int -> 'acc -> 'acc) -> ?start:int -> string -> 'acc -> 'accโrfind_all ~sub f ~start s acc, starting withacc, foldsfin decreasing index order over all non-overlapping starting positions ofsubinsat or before the index or positionstart(defaults toString.length s). The result isaccifsubcouldn't be found ins. Ifsubis"",fgets invoked on all positions ofsat or beforestart.
Since 5.5
Raises Invalid_argument if start is not a valid position of s. ๐
๐ Replacing substrings
Note. Same energy as before: if you're replacing the same sub string multiple times, partially applying the ~sub argument of these functions and reusing the resulting function repeatedly is more efficient. ๐
val replace_first : sub:string -> by:string -> ?start:int -> string -> stringโ okay soreplace_first ~sub ~by ~start sreplacesbyby the first occurrence ofsubinsat or after the index or positionstart(defaults to0). Ifsubis"", this insertsbyat positionstart. โจ
Since 5.5
Raises Invalid_argument if start is not a valid position of s. ๐
val replace_last : sub:string -> by:string -> ?start:int -> string -> stringโreplace_last ~sub ~by ~start sreplacesbyby the last occurrence ofsubinsat or after the index or positionstart(defaults toString.length s). Ifsubis"", this insertsbyat positionstart. ๐ธ
Since 5.5
Raises Invalid_argument if start is not a valid position of s. ๐
val replace_all : sub:string -> by:string -> ?start:int -> string -> stringโreplace_all ~sub ~by ~start sreplacesbyby all the non-overlapping occurrences ofsubinsat or after the index or positionstart(defaults to0). Occurrences are found in increasing indexing order. Ifsubis"", this insertsbyon all positions fromstarton. ate ๐
Since 5.5
Raises Invalid_argument if start is not a valid position of s. ๐
๐ Strings and Sequences
val to_seq : t -> char Seq.tโto_seq sis a sequence made of the string's characters in increasing order.
Since 4.07
val to_seqi : t -> (int * char) Seq.tโto_seqi sis likeStringLabels.to_seqbut it also tuples the corresponding index. iykyk ๐ซถ
Since 4.07
val of_seq : char Seq.t -> tโof_seq sis a string made of the sequence's characters.
Since 4.07
๐ UTF decoding and validations
๐ UTF-8
val get_utf_8_uchar : t -> int -> Uchar.utf_decodeโget_utf_8_uchar b idecodes an UTF-8 character at indexiinb.val is_valid_utf_8 : t -> boolโis_valid_utf_8 bistrueif and only ifbcontains valid UTF-8 data. bestie either it's valid or it's not, no in-between ๐
๐ UTF-16BE
val get_utf_16be_uchar : t -> int -> Uchar.utf_decodeโget_utf_16be_uchar b idecodes an UTF-16BE character at indexiinb.val is_valid_utf_16be : t -> boolโis_valid_utf_16be bistrueif and only ifbcontains valid UTF-16BE data.
๐ง UTF-16LE
val get_utf_16le_uchar : t -> int -> Uchar.utf_decodeโget_utf_16le_uchar b idecodes an UTF-16LE character at indexiinb.val is_valid_utf_16le : t -> boolโis_valid_utf_16le bistrueif and only ifbcontains valid UTF-16LE data.
๐ค Spellchecking
val edit_distance : ?limit:int -> t -> t -> intโedit_distance s0 s1is the number of single character edits needed to changes0intos1. "Single character edit" here means insertion, deletion, substitution, or transposition. ๐
If limit is provided, the function returns with limit as soon as it's determined that s0 and s1 have a distance of at least limit. This is faster if you've got a fixed limit, for example for spellchecking. ๐ฅ
The function assumes the strings are UTF-8 encoded and uses Uchar.t for the notion of character. Decoding errors get replaced by Uchar.rep. Normalizing the strings to NFC gives better results.
Note. This implements the simpler Optimal String Alignment (OSA) distance, not the Damerau-Levenshtein distance. With this function"ca"and"abc"have a distance of3not2. don't come for her, she told you upfront ๐
Since 5.4
val spellcheck : ?max_dist:(string -> int) -> ((string -> unit) -> unit) -> string -> string listโspellcheck iter_dict sare the strings enumerated by the iteratoriter_dictwhoseStringLabels.edit_distancetosis the smallest and at mostmax_dist s. If multiple corrections are returned, their order is as found initer_dict. The defaultmax_dist sis:0ifshas 0 to 2 Unicode characters.1ifshas 3 to 4 Unicode characters.2otherwise.
If your dictionary is a list l, a suitable iter_dict is given by:
(fun yield -> List.iter yield l)
All strings are assumed to be UTF-8 encoded, and decoding errors are replaced by Uchar.rep characters. ๐ฆ
Since 5.4
๐งฎ Binary decoding of integers
The functions in this section binary decode integers from strings. ngl this is the part where the girlies either lock in or leave ๐ค
All the 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 the 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 represent them with int values. ๐ซถ
val get_uint8 : string -> int -> intโget_uint8 b iisb's unsigned 8-bit integer starting at character indexi.
Since 4.13
val get_int8 : string -> int -> intโget_int8 b iisb's signed 8-bit integer starting at character indexi.
Since 4.13
val get_uint16_ne : string -> int -> intโget_uint16_ne b iisb's native-endian unsigned 16-bit integer starting at character indexi.
Since 4.13
val get_uint16_be : string -> int -> intโget_uint16_be b iisb's big-endian unsigned 16-bit integer starting at character indexi.
Since 4.13
val get_uint16_le : string -> int -> intโget_uint16_le b iisb's little-endian unsigned 16-bit integer starting at character indexi.
Since 4.13
val get_int16_ne : string -> int -> intโget_int16_ne b iisb's native-endian signed 16-bit integer starting at character indexi.
Since 4.13
val get_int16_be : string -> int -> intval get_int16_be : string -> int -> intโget_int16_be b iisb's big-endian signed 16-bit integer starting at character indexi. (Since 4.13)val get_int16_le : string -> int -> intโget_int16_le b iisb's little-endian signed 16-bit integer starting at character indexi. (Since 4.13)val get_int32_ne : string -> int -> int32โget_int32_ne b iisb's native-endian 32-bit integer starting at character indexi. (Since 4.13)val hash : t -> intโ An unseeded hash function for strings, with the same output value asHashtbl.hash. This function allows this module to be passed as argument to the functorHashtbl.Make. (Since 5.0)val seeded_hash : int -> t -> intโ A seeded hash function for strings, with the same output value asHashtbl.seeded_hash. This function allows this module to be passed as argument to the functorHashtbl.MakeSeeded. (Since 5.0)val get_int32_be : string -> int -> int32โget_int32_be b iisb's big-endian 32-bit integer starting at character indexi. (Since 4.13)val get_int32_le : string -> int -> int32โget_int32_le b iisb's little-endian 32-bit integer starting at character indexi. (Since 4.13)val get_int64_ne : string -> int -> int64โget_int64_ne b iisb's native-endian 64-bit integer starting at character indexi. (Since 4.13)val get_int64_be : string -> int -> int64โget_int64_be b iisb's big-endian 64-bit integer starting at character indexi. (Since 4.13)val get_int64_le : string -> int -> int64โget_int64_le b iisb's little-endian 64-bit integer starting at character indexi. (Since 4.13)
and thatโs the whole byte-reading + hashing crew, bestie ๐ซถ this page was served up by OCamldoc, with the date stamp 2026-06-22, for StdLabels.String(3) โ periodt ๐
โจ
๐ฆ where this page comes from
- arch rolling: ocaml 5.5.0-1 ยท LGPL2.1 AND custom: QPL-1.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms