Module B0_std.String
Strings.
String
include module type of Stdlib.String
val get : string -> int -> charString.get s nreturns the character at indexnin strings. You can also writes.[n]instead ofString.get s n.Raise
Invalid_argumentifnnot a valid index ins.
val set : bytes -> int -> char -> unitString.set s n cmodifies byte sequencesin place, replacing the byte at indexnwithc. You can also writes.[n] <- cinstead ofString.set s n c.Raise
Invalid_argumentifnis not a valid index ins.- deprecated
This is a deprecated alias of
Bytes.set.
val create : int -> bytesString.create nreturns a fresh byte sequence of lengthn. The sequence is uninitialized and contains arbitrary bytes.Raise
Invalid_argumentifn < 0orn >Sys.max_string_length.- deprecated
This is a deprecated alias of
Bytes.create.
val make : int -> char -> stringString.make n creturns a fresh string of lengthn, filled with the characterc.Raise
Invalid_argumentifn < 0orn >Sys.max_string_length.
val init : int -> (int -> char) -> stringString.init n freturns a string of lengthn, with characteriinitialized to the result off i(called in increasing index order).Raise
Invalid_argumentifn < 0orn >Sys.max_string_length.- since
- 4.02.0
val copy : string -> stringReturn a copy of the given string.
- deprecated
Because strings are immutable, it doesn't make much sense to make identical copies of them.
val sub : string -> int -> int -> stringString.sub s start lenreturns a fresh string of lengthlen, containing the substring ofsthat starts at positionstartand has lengthlen.Raise
Invalid_argumentifstartandlendo not designate a valid substring ofs.
val fill : bytes -> int -> int -> char -> unitString.fill s start len cmodifies byte sequencesin place, replacinglenbytes withc, starting atstart.Raise
Invalid_argumentifstartandlendo not designate a valid range ofs.- deprecated
This is a deprecated alias of
Bytes.fill.
val concat : string -> string list -> stringString.concat sep slconcatenates the list of stringssl, inserting the separator stringsepbetween each.Raise
Invalid_argumentif the result is longer thanSys.max_string_length bytes.
val iter : (char -> unit) -> string -> unitString.iter f sapplies functionfin turn to all the characters ofs. It is equivalent tof s.[0]; f s.[1]; ...; f s.[String.length s - 1]; ().
val iteri : (int -> char -> unit) -> string -> unitSame as
String.iter, but the function is applied to the index of the element as first argument (counting from 0), and the character itself as second argument.- since
- 4.00.0
val map : (char -> char) -> string -> stringString.map f sapplies functionfin turn to all the characters ofs(in increasing index order) and stores the results in a new string that is returned.- since
- 4.00.0
val mapi : (int -> char -> char) -> string -> stringString.mapi f scallsfwith each character ofsand its index (in increasing index order) and stores the results in a new string that is returned.- since
- 4.02.0
val trim : string -> stringReturn a copy of the argument, without leading and trailing whitespace. The characters regarded as whitespace are:
' ','\012','\n','\r', and'\t'. If there is neither leading nor trailing whitespace character in the argument, return the original string itself, not a copy.- since
- 4.00.0
val escaped : string -> stringReturn a copy of the argument, with special characters represented by escape sequences, following the lexical conventions of OCaml. All characters outside the ASCII printable range (32..126) are escaped, as well as backslash and double-quote.
If there is no special character in the argument that needs escaping, return the original string itself, not a copy.
Raise
Invalid_argumentif the result is longer thanSys.max_string_length bytes.The function
Scanf.unescaped is a left inverse ofescaped, i.e.Scanf.unescaped (escaped s) = sfor any strings(unlessescape sfails).
val index : string -> char -> intString.index s creturns the index of the first occurrence of charactercin strings.Raise
Not_foundifcdoes not occur ins.
val index_opt : string -> char -> int optionString.index_opt s creturns the index of the first occurrence of charactercin strings, orNoneifcdoes not occur ins.- since
- 4.05
val rindex : string -> char -> intString.rindex s creturns the index of the last occurrence of charactercin strings.Raise
Not_foundifcdoes not occur ins.
val rindex_opt : string -> char -> int optionString.rindex_opt s creturns the index of the last occurrence of charactercin strings, orNoneifcdoes not occur ins.- since
- 4.05
val index_from : string -> int -> char -> intString.index_from s i creturns the index of the first occurrence of charactercin stringsafter positioni.String.index s cis equivalent toString.index_from s 0 c.Raise
Invalid_argumentifiis not a valid position ins. RaiseNot_foundifcdoes not occur insafter positioni.
val index_from_opt : string -> int -> char -> int optionString.index_from_opt s i creturns the index of the first occurrence of charactercin stringsafter positioniorNoneifcdoes not occur insafter positioni.String.index_opt s cis equivalent toString.index_from_opt s 0 c. RaiseInvalid_argumentifiis not a valid position ins.- since
- 4.05
val rindex_from : string -> int -> char -> intString.rindex_from s i creturns the index of the last occurrence of charactercin stringsbefore positioni+1.String.rindex s cis equivalent toString.rindex_from s (String.length s - 1) c.Raise
Invalid_argumentifi+1is not a valid position ins. RaiseNot_foundifcdoes not occur insbefore positioni+1.
val rindex_from_opt : string -> int -> char -> int optionString.rindex_from_opt s i creturns the index of the last occurrence of charactercin stringsbefore positioni+1orNoneifcdoes not occur insbefore positioni+1.String.rindex_opt s cis equivalent toString.rindex_from_opt s (String.length s - 1) c.Raise
Invalid_argumentifi+1is not a valid position ins.- since
- 4.05
val contains : string -> char -> boolString.contains s ctests if charactercappears in the strings.
val contains_from : string -> int -> char -> boolString.contains_from s start ctests if charactercappears insafter positionstart.String.contains s cis equivalent toString.contains_from s 0 c.Raise
Invalid_argumentifstartis not a valid position ins.
val rcontains_from : string -> int -> char -> boolString.rcontains_from s stop ctests if charactercappears insbefore positionstop+1.Raise
Invalid_argumentifstop < 0orstop+1is not a valid position ins.
val uppercase : string -> stringReturn a copy of the argument, with all lowercase letters translated to uppercase, including accented letters of the ISO Latin-1 (8859-1) character set.
- deprecated
Functions operating on Latin-1 character set are deprecated.
val lowercase : string -> stringReturn a copy of the argument, with all uppercase letters translated to lowercase, including accented letters of the ISO Latin-1 (8859-1) character set.
- deprecated
Functions operating on Latin-1 character set are deprecated.
val capitalize : string -> stringReturn a copy of the argument, with the first character set to uppercase, using the ISO Latin-1 (8859-1) character set..
- deprecated
Functions operating on Latin-1 character set are deprecated.
val uncapitalize : string -> stringReturn a copy of the argument, with the first character set to lowercase, using the ISO Latin-1 (8859-1) character set..
- deprecated
Functions operating on Latin-1 character set are deprecated.
val uppercase_ascii : string -> stringReturn a copy of the argument, with all lowercase letters translated to uppercase, using the US-ASCII character set.
- since
- 4.03.0
val lowercase_ascii : string -> stringReturn a copy of the argument, with all uppercase letters translated to lowercase, using the US-ASCII character set.
- since
- 4.03.0
val capitalize_ascii : string -> stringReturn a copy of the argument, with the first character set to uppercase, using the US-ASCII character set.
- since
- 4.03.0
val uncapitalize_ascii : string -> stringReturn a copy of the argument, with the first character set to lowercase, using the US-ASCII character set.
- since
- 4.03.0
val compare : t -> t -> intThe comparison function for strings, with the same specification as
Stdlib.compare. Along with the typet, this functioncompareallows the moduleStringto be passed as argument to the functorsSet.Make andMap.Make.
val split_on_char : char -> string -> string listString.split_on_char sep sreturns the list of all (possibly empty) substrings ofsthat are delimited by thesepcharacter.The function's output is specified by the following invariants:
- The list is not empty.
- Concatenating its elements using
sepas a separator returns a string equal to the input (String.concat (String.make 1 sep) (String.split_on_char sep s) = s). - No string in the result contains the
sepcharacter.
- since
- 4.04.0
Iterators
val to_seq : t -> char Stdlib.Seq.tIterate on the string, in increasing index order. Modifications of the string during iteration will be reflected in the iterator.
- since
- 4.07
val to_seqi : t -> (int * char) Stdlib.Seq.tIterate on the string, in increasing order, yielding indices along chars
- since
- 4.07
val of_seq : char Stdlib.Seq.t -> tCreate a string from the generator
- since
- 4.07
Predicates
val is_prefix : affix:string -> string -> boolis_prefix ~affix sistrueiffaffix.[i] = s.[i]for all indicesiofaffix.
val is_infix : affix:string -> string -> boolis_infix ~affix sistrueiff there exists an indexjsuch that for all indicesiofaffix,affix.[i] = s.[j+ 1].
val is_suffix : affix:string -> string -> boolis_suffix ~affix sis true iffaffix.[i] = s.[m - i]for all indicesiofaffixand withm = String.length s - 1.
Extracting substrings
val with_index_range : ?first:int -> ?last:int -> string -> stringwith_index_range ~first ~last sare the consecutive bytes ofswhose indices exist in the range [first;last].firstdefaults to0and last toString.length s - 1.Note that both
firstandlastcan be any integer. Iffirst > lastthe interval is empty and the empty string is returned.
Breaking
Breaking with magnitudes
val take_left : int -> string -> stringtake_left n sare the firstnbytes ofs. This issifn >= length sand""ifn <= 0.
val take_right : int -> string -> stringtake_right n sare the lastnbytes ofs. This issifn >= length sand""ifn <= 0.
val drop_left : int -> string -> stringdrop_left n sisswithout the firstnbytes ofs. This is""ifn >= length sandsifn <= 0.
Breaking with predicates
val keep_left : (char -> bool) -> string -> stringkeep_left sat sare the first consecutivesatstatisfying bytes ofs.
val keep_right : (char -> bool) -> string -> stringkeep_right sat sare the last consecutivesatsatisfying bytes ofs.
val lose_left : (char -> bool) -> string -> stringlose_left sat sisswithout the first consecutivesatsatisfying bytes ofs.
val lose_right : (char -> bool) -> string -> stringlose_right sat sisswithout the last consecutivesatsatisfying bytes ofs.
Breaking with separators
val cut_left : sep:string -> string -> (string * string) optioncut ~sep sis either the pairSome (l,r)of the two (possibly empty) substrings ofsthat are delimited by the first match of the separator characterseporNoneifsepcan't be matched ins. Matching starts from the left ofs.The invariant
l ^ sep ^ r = sholds.- raises Invalid_argument
if
sepis the empty string.
val cut_right : sep:string -> string -> (string * string) optioncut_right ~sep sis likecut_leftbut matching starts on the right ofs.
val cuts_left : ?drop_empty:bool -> sep:string -> string -> string listcuts_left sep sis the list of all substrings ofsthat are delimited by matches of the non empty separator stringsep. Empty substrings are omitted in the list ifdrop_emptyistrue(defaults tofalse).Matching separators in
sstarts from the left ofs(revisfalse, default) or the end (revistrue). Once one is found, the separator is skipped and matching starts again, that is separator matches can't overlap. If there is no separator match ins, the list[s]is returned.The following invariants hold:
concat ~sep (cuts ~drop_empty:false ~sep s) = scuts ~drop_empty:false ~sep s <> []
- raises Invalid_argument
if
sepis the empty string.
val cuts_right : ?drop_empty:bool -> sep:string -> string -> string listcuts_right sep sis likecuts_leftbut matching starts on the right ofs.
Traversing
Formatting
val pp : string Fmt.tpp ppf sprintss's bytes onppf.
val dump : string Fmt.tdump ppf sprintssas a syntactically valid OCaml string onppf.
Uniqueness
val uniquify : string list -> string listuniquify ssissswithout duplicates, the list order is preserved.
val unique : exists:(string -> bool) -> string -> (string, string) Stdlib.resultunique ~exist nisnifexists nisfalseorr = strf "%s~%d" n dwithdthe smallest integer in [1;1e9] such thatexists risfalseor an error if there is no such string.
Suggesting
val edit_distance : string -> string -> intedit_distance s0 s1is the number of single character edits (insertion, deletion, substitution) that are needed to changes0intos1.
val suggest : ?dist:int -> string list -> string -> string listsuggest ~dist candidates sare the elements ofcandidateswhose edit distance is the smallest tosand at most at a distance ofdistofs(defaults to2). If multiple results are returned the order ofcandidatesis preserved.
Escaping and unescaping bytes
See also the Converting to printable US-ASCII characters.
XXX. Limitation cannot escape/unescape multiple bytes (e.g. UTF-8 byte sequences). This could be achieved by tweaking the sigs to return integer pairs but that would allocate quite a bit.
val escaper : (char -> int) -> (bytes -> int -> char -> int) -> string -> stringescaper char_len set_charis a byte escaper that given a bytecuseschar_len cbytes in the escaped form and usesset_char b i cto set the escaped form forcinbat indexireturning the next writable index (no bounds check need to be performed). For anyb,candithe invarianti + char_len c = set_char b i cmust hold.
exceptionIllegal_escape of intSee
unescaper.
val unescaper : (string -> int -> int) -> (bytes -> int -> string -> int -> int) -> string -> (string, int) Stdlib.resultunescaper char_len_at set_charis a byte unescaper that useschar_len_atto determine the length of a byte at a given index in the string to unescape andset_char b k s ito set at indexkinbthe unescaped character read at indexiins; and returns the next readable index ins(no bound check need to be performed). For anyb,s,kandithe invarianti + char_len_at s i = set_char b k s i.Both
char_len_atandset_charmay raiseIllegal_escape iif the given indexihas an illegal or truncated escape. The unescaper only uses this exception internally it returnsError iif it found an illegal escape at indexi.
Strings as US-ASCII character sequences
module Ascii : sig ... endUS-ASCII string support.
String map and sets
module Set : sig ... endString sets.
module Map : sig ... endString maps.