Given a string s of length l, we call character number in s the index of a character in s. Indexes start at 0, and we will call a character number valid in s if it falls within the range [0...l-1]. A position is the point between two characters or at the beginning or end of the string. We call a position valid in s if it falls within the range [0...l]. Note that character number n is between positions n and n+1.
Two parameters start and len are said to designate a valid substring of s if len >= 0 and start and start+len are valid positions in s.
If you're going to do a lot of string slicing, BatSubstring might be a useful module to represent slices of strings, as it doesn't allocate new strings on every operation.
for_all p s check if all chars in s satisfy the predicate p.
since 3.4.0
val length : string -> int
Return the length (number of characters) of the given string.
val get : string ->int -> char
String.get s n returns character number n in string s. You can also write s.[n] instead of String.get s n.
raisesInvalid_argument
if n not a valid character number in s.
val set : Stdlib.Bytes.t ->int ->char -> unit
String.set s n c modifies string s in place, replacing the character number n by c. You can also write s.[n] <- c instead of String.set s n c.
raisesInvalid_argument
if n is not a valid character number in s.
val create : int ->Stdlib.Bytes.t
String.create n returns a fresh string of length n. The string initially contains arbitrary characters.
raisesInvalid_argument
if n < 0 or n > Sys.max_string_length.
val make : int ->char -> string
String.make n c returns a fresh string of length n, filled with the character c.
raisesInvalid_argument
if n < 0 or n > Sys.max_string_length.
val copy : string -> string
Return a copy of the given string.
val sub : string ->int ->int -> string
String.sub s start len returns a fresh string of length len, containing the substring of s that starts at position start and has length len.
raisesInvalid_argument
if start and len do not designate a valid substring of s.
val fill : Stdlib.Bytes.t ->int ->int ->char -> unit
String.fill s start len c modifies the byte sequence s in place, replacing len characters by c, starting at start.
raisesInvalid_argument
if start and len do not designate a valid substring of s.
val blit : string ->int ->Stdlib.Bytes.t ->int ->int -> unit
String.blit src srcoff dst dstoff len copies len characters from string src, starting at character number srcoff, to the byte sequence dst, starting at character number dstoff.
raisesInvalid_argument
if srcoff and len do not designate a valid substring of src, or if dstoff and len do not designate a valid substring of dst.
val concat : string ->string list-> string
String.concat sep sl concatenates the list of strings sl, inserting the separator string sep between each.
val iter : (char -> unit)->string -> unit
String.iter f s applies function f in turn to all the characters of s. It is equivalent to f s.[0]; f s.[1]; ...; f s.[String.length s - 1]; ().
Return a copy of the argument, without leading and trailing whitespace (according to BatChar.is_whitespace). The characters regarded as whitespace are: ' ', '\n', '\r', '\t', '\012' and '\026'. If there is no 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. If there is no special character in the argument, return the original string itself, not a copy. Its inverse function is Scanf.unescaped.
val index : string ->char -> int
String.index s c returns the character number of the first occurrence of character c in string s.
raisesNot_found
if c does not occur in s.
val index_opt : string ->char ->int option
String.index_opt s c returns the index of the first occurrence of character c in string s, or None if c does not occur in s.
since 2.7.0
val rindex : string ->char -> int
String.rindex s c returns the character number of the last occurrence of character c in string s.
raisesNot_found
if c does not occur in s.
val rindex_opt : string ->char ->int option
String.rindex_opt s c returns the index of the last occurrence of character c in string s, or None if c does not occur in s.
since 2.7.0
val index_from : string ->int ->char -> int
String.index_from s i c returns the character number of the first occurrence of character c in string s after or at position i. String.index s c is equivalent to String.index_from s 0 c.
raisesInvalid_argument
if i is not a valid position in s.
raisesNot_found
if c does not occur in s after position i.
val index_from_opt : string ->int ->char ->int option
String.index_from_opt s i c returns the index of the first occurrence of character c in string s after position i or None if c does not occur in s after position i.
String.index_opt s c is equivalent to String.index_from_opt s 0 c. Raise Invalid_argument if i is not a valid position in s.
since 2.7.0
val rindex_from : string ->int ->char -> int
String.rindex_from s i c returns the character number 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.
raisesInvalid_argument
if i+1 is not a valid position in s.
raisesNot_found
if c does not occur in s before position i+1.
val rindex_from_opt : string ->int ->char ->int option
String.rindex_from_opt s i c returns the index of the last occurrence of character c in string s before position i+1 or None if c does not occur in s before position i+1.
String.rindex_opt s c is equivalent to String.rindex_from_opt s (String.length s - 1) c.
Raise Invalid_argument if i+1 is not a valid position in s.
since 2.7.0
Sourceval index_after_n : char ->int ->string -> int
index_after_n chr n str returns the index of the character that comes immediately after the n-th occurrence of chr in str.
Occurrences are numbered from 1: n = 1 returns the index of the character located immediately after the first occurrence of chr.
n = 0 always returns 0.
If the n-th occurrence of chr is the last character of str, returns the length of str.
raisesInvalid_argument
if n < 0.
raisesNot_found
if there are strictly less than n occurrences of chr in str.
since 2.9.0
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.
raisesInvalid_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.
raisesInvalid_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.
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.
val capitalize : string -> string
Return a copy of the argument, with the first character set to uppercase.
val uncapitalize : string -> string
Return a copy of the argument, with the first character set to lowercase.
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 2.5.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 2.5.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 2.5.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.
Returns the integer represented by the given string. This follows OCaml's int literal rules, so "0x" prefixes hexadecimal integers, "0o" for octal and "0b" for binary. Underscores within the number are allowed for readability but ignored.
Returns the float represented by the given string. Decimal points aren't required in the given string, as they are for float literals in OCaml, but otherwise the rules for float literals apply.
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).
Example: String.filter_map (function 'a'..'z' as c -> Some (Char.uppercase c) | _ -> None) "a b c" = "ABC"
Sourceval iteri : (int ->char -> unit)->string -> unit
String.iteri f s is equivalent to f 0 s.[0]; f 1 s.[1]; ...; f len s.[len] where len is length of string s. Example:
let letter_positions word =
let positions = Array.make 256 [] in
let count_letter pos c =
positions.(int_of_char c) <- pos :: positions.(int_of_char c) in
String.iteri count_letter word;
Array.mapi (fun c pos -> (char_of_int c, List.rev pos)) positions
|> Array.to_list
|> List.filter (fun (c,pos) -> pos <> [])
in
letter_positions "hello" = ['e',[1]; 'h',[0]; 'l',[2;3]; 'o',[4] ]
rfind s x returns the starting index of the last occurrence of string x within string s.
Note This implementation is optimized for short strings.
raisesNot_found
if x is not a substring of s.
Example: String.rfind "foobarbaz" "ba" = 6
Sourceval rfind_from : string ->int ->string -> int
rfind_from s pos x behaves as rfind s x but starts searching from the right at position pos + 1. rfind s x is equivalent to rfind_from s (String.length s - 1) x.
Beware, it search between the beginning of the string to the position pos + 1, not between pos + 1 and the end.
Add quotes around a string and escape any quote or escape appearing in that string. This function is used typically when you need to generate source code from a string.
More precisely, the returned string conforms to the OCaml syntax: if printed, it outputs a representation of the input string as an OCaml string litteral.
replace ~str ~sub ~by returns a tuple consisting of a boolean and a string where the first occurrence of the string sub within str has been replaced by the string by. The boolean is true if a substitution has taken place.
nreplace ~str ~sub ~by returns a string obtained by iteratively replacing each occurrence of sub by by in str, from right to left. It returns a copy of str if sub has no occurrence in str.
rev_in_place s mutates the byte sequence s, so that its new value is the mirror of its old one: for instance if s contained "Example!", after the mutation it will contain "!elpmaxE".
Sourceval in_place_mirror : Stdlib.Bytes.t -> unit
deprecated
Use String.rev_in_place instead
Splitting around
Sourceval split_on_char : char ->string ->string list
String.split_on_char sep s returns the list of all (possibly empty) substrings of s that are delimited by the sep character.
The function's output 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 (String.concat (String.make 1 sep) (String.split_on_char sep s) = s).
No string in the result contains the sep character.
Note: prior to 2.11.0 split_on_char _ "" used to return an empty list.
Sourceval nsplit : string ->by:string ->string list
nsplit s sep splits the string s into a list of strings which are separated by sep (excluded). nsplit "" _ returns a single empty string. Note: prior to 2.11.0 nsplit "" _ used to return an empty list.
Sourceval split_on_string : by:string ->string ->string list
split_on_string sep s splits the string s into a list of strings which are separated by sep (excluded). split_on_string _ "" returns a single empty string. Note: split_on_string sep s is identical to nsplit s sep but for empty strings.
slice ?first ?last s returns a "slice" of the string which corresponds to the characters s.[first], s.[first+1], ..., s[last-1]. Note that the character at index last is not included! If first is omitted it defaults to the start of the string, i.e. index 0, and if last is omitted is defaults to point just past the end of s, i.e. length s. Thus, slice s is equivalent to copy s.
Negative indexes are interpreted as counting from the end of the string. For example, slice ~last:(-2) s will return the string s, but without the last two characters.
This function never raises any exceptions. If the indexes are out of bounds they are automatically clipped.
Example: String.slice ~first:1 ~last:(-3) " foo bar baz" = "foo bar "
String.splice s off len rep cuts out the section of s indicated by off and len and replaces it by rep
Negative indexes are interpreted as counting from the end of the string. If off+len is greater than length s, the end of the string is used, regardless of the value of len.
If len is zero or negative, rep is inserted at position off without replacing any of s.
Example: String.splice "foo bar baz" 3 5 "XXX" = "fooXXXbaz"
The 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.
The comparison function for strings, with the same specification as Pervasives.compare. Along with the type t, this function compare allows the module String to be passed as argument to the functors Set.Make and Map.Make.
Example: String.compare "FOO" "bar" = -1 i.e. "FOO" < "bar"
Compare two strings, sorting "abc32def" before "abc210abc".
Algorithm: splits both strings into lists of (strings of digits) or (strings of non digits) (["abc"; "32"; "def"] and ["abc"; "210"; "abc"]) Then both lists are compared lexicographically by comparing elements numerically when both are numbers or lexicographically in other cases.