Same 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 -> string
String.map f s applies function f in turn to all the characters of s (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 -> string
String.mapi f s calls f with each character of s and its index (in increasing index order) and stores the results in a new string that is returned.
since 4.02.0
val trim : string -> string
Return 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 -> string
Return 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.
The function Scanf.unescaped is a left inverse of escaped, i.e. Scanf.unescaped (escaped s) = s for any string s (unless escape s fails).
val index : string ->char -> int
String.index s c returns the index of the first occurrence of character c in string s.
Raise Not_found if c does not occur in s.
val rindex : string ->char -> int
String.rindex s c returns the index of the last occurrence of character c in string s.
Raise Not_found if c does not occur in s.
val index_from : string ->int ->char -> int
String.index_from s i c returns the index of the first occurrence of character c in string s after position i. String.index s c is equivalent to String.index_from s 0 c.
Raise Invalid_argument if i is not a valid position in s. Raise Not_found if c does not occur in s after position i.
val rindex_from : string ->int ->char -> int
String.rindex_from s i c returns the index of the last occurrence of character c in string s before position i+1. String.rindex s c is equivalent to String.rindex_from s (String.length s - 1) c.
Raise Invalid_argument if i+1 is not a valid position in s. Raise Not_found if c does not occur in s before position i+1.
val contains : string ->char -> bool
String.contains s c tests if character c appears in the string s.
val contains_from : string ->int ->char -> bool
String.contains_from s start c tests if character c appears in s after position start. String.contains s c is equivalent to String.contains_from s 0 c.
Raise Invalid_argument if start is not a valid position in s.
val rcontains_from : string ->int ->char -> bool
String.rcontains_from s stop c tests if character c appears in s before position stop+1.
Raise Invalid_argument if stop < 0 or stop+1 is not a valid position in s.
val uppercase : string -> string
Return 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 -> string
Return 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 -> string
Return 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 -> string
Return 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 -> string
Return 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 -> string
Return 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 -> string
Return 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 -> string
Return a copy of the argument, with the first character set to lowercase, using the US-ASCII character set.
blit 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.
replace ~which ~sub ~by s replaces some occurrences of sub by by in s.
parameterwhich
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).
raisesInvalid_argument
if sub = "".
since 0.14
val is_sub : sub:string ->int ->string ->int ->sub_len:int -> bool
is_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.
val repeat : string ->int -> string
repeat s n creates a string by repeating the string sn times.
val prefix : pre:string ->string -> bool
prefix ~pre s returns true iff pre is a prefix of s.
val suffix : suf:string ->string -> bool
suffix ~suf s returns true iff suf is a suffix of s.
since 0.7
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.
since 0.17
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.
since 0.17
val take : int ->string -> string
take n s keeps only the n first chars of s.
since 0.17
val drop : int ->string -> string
drop n s removes the n first chars of s.
since 0.17
val take_drop : int ->string -> string * string
take_drop n s is take n s, drop n s.
since 0.17
val lines : string ->string list
lines s returns a list of the lines of s (splits along '\n').
unlines_seq seq concatenates all strings of seq, separated with '\n'.
since 3.2
val set : string ->int ->char -> string
set s i c creates a new string which is a copy of s, except for index i, which becomes c.
raisesInvalid_argument
if i is an invalid index.
since 0.12
val iter : (char -> unit)->string -> unit
iter f s applies function f on each character of s. Alias to String.iter.
since 0.12
val filter_map : (char ->char option)->string -> string
filter_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).
since 0.17
val filter : (char -> bool)->string -> string
filter f s discards characters of s not satisfying f.
since 0.17
val uniq : (char ->char -> bool)->string -> string
uniq eq s remove consecutive duplicate characters in s.
since 3.4
val flat_map : ?sep:string ->(char -> string)->string -> string
flat_map ~sep f s maps each chars of s to a string, then concatenates them all.
parametersep
optional separator between each generated string.
since 0.12
val for_all : (char -> bool)->string -> bool
for_all f s is true iff all characters of s satisfy the predicate f.
since 0.12
val exists : (char -> bool)->string -> bool
exists f s is true iff some character of s satisfy the predicate f.
split_on_char by s splits the string s along the given char by.
since 1.2
val split : by:string ->string ->string list
split ~by s splits the string s along the given string by. Alias to Split.list_cpy.
since 1.2
Utils
val compare_versions : string ->string -> int
compare_versions s1 s2 compares version stringss1 and s2, considering that numbers are above text.
since 0.13
val compare_natural : string ->string -> int
compare_natural s1 s2 is 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 -> int
edit_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.
parametercutoff
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 than cutoff if it's reached in <cutoff iterations. However if the result is < cutoff then it is accurate.