module BatString:sig..end
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.
OCaml strings can be modified in place, for instance via the
String.set and String.blit functions described below. This
possibility should be used rarely and with much care, however, since
both the OCaml compiler and most OCaml libraries share strings as if
they were immutable, rather than copying them. In particular,
string literals are shared: a single copy of the string is created
at program loading time and returned by all evaluations of the
string literal. Consider for example:
# let f () = "foo";;
val f : unit -> string = <fun>
# (f ()).[0] <- 'b';;
-: unit = ()
# f ();;
-: string = "boo"
Likewise, many functions from the standard library can return string
literals or one of their string arguments. Therefore, the returned strings
must not be modified directly. If mutation is absolutely necessary,
it should be performed on a fresh copy of the string, as produced by
String.copy.
This module replaces Stdlib's String module.
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.
Author(s): Xavier Leroy (base library), Nicolas Cannasse, David Teller, Edgar Friendly
val init : int -> (int -> char) -> stringinit l f returns the string of length l with the chars
f 0 , f 1 , f 2 ... f (l-1).
Example: String.init 256 char_of_int
val is_empty : string -> boolis_empty s returns true if s is the empty string, false
otherwise.
Usually a tad faster than comparing s with "".
Example (for some string s):
if String.is_empty s then "(Empty)" else s
val length : string -> intval get : string -> int -> charString.get s n returns character number n in string s.
You can also write s.[n] instead of String.get s n.Invalid_argument if n not a valid character number in s.val set : string -> int -> char -> unitString.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.Invalid_argument if n is not a valid character number in s.val create : int -> stringString.create n returns a fresh string of length n.
The string initially contains arbitrary characters.Invalid_argument if n < 0 or n > Sys.max_string_length.val make : int -> char -> stringString.make n c returns a fresh string of length n,
filled with the character c.Invalid_argument if n < 0 or n > Sys.max_string_length.val copy : string -> stringval sub : string -> int -> int -> stringString.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.Invalid_argument if start and len do not
designate a valid substring of s.val fill : string -> int -> int -> char -> unitString.fill s start len c modifies string s in place,
replacing len characters by c, starting at start.Invalid_argument if start and len do not
designate a valid substring of s.val blit : string -> int -> string -> int -> int -> unitString.blit src srcoff dst dstoff len copies len characters
from string src, starting at character number srcoff, to
string dst, starting at character number dstoff. It works
correctly even if src and dst are the same string,
and the source and destination intervals overlap.Invalid_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 -> stringString.concat sep sl concatenates the list of strings sl,
inserting the separator string sep between each.val iter : (char -> unit) -> string -> unitString.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]; ().val iteri : (int -> char -> unit) -> string -> unitString.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.val map : (char -> char) -> string -> stringString.map f s applies function f in turn to all
the characters of s and stores the results in a new string that
is returned.val mapi : (int -> char -> char) -> string -> stringString.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.val trim : string -> string' ',
'\012', '\n', '\r', and '\t'. If there is no leading nor
trailing whitespace character in the argument, return the original
string itself, not a copy.val escaped : string -> stringval index : string -> char -> intString.index s c returns the character number of the first
occurrence of character c in string s.Not_found if c does not occur in s.val rindex : string -> char -> intString.rindex s c returns the character number of the last
occurrence of character c in string s.Not_found if c does not occur in s.val index_from : string -> int -> char -> intString.index_from s i c returns the character number 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.Invalid_argument if i is not a valid position in s.Not_found if c does not occur in s after position i.val rindex_from : string -> int -> char -> intString.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.Invalid_argument if i+1 is not a valid position in s.Not_found if c does not occur in s before position i+1.val contains : string -> char -> boolString.contains s c tests if character c
appears in the string s.val contains_from : string -> int -> char -> boolString.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.Invalid_argument if start is not a valid position in s.val rcontains_from : string -> int -> char -> boolString.rcontains_from s stop c tests if character c
appears in s before position stop+1.Invalid_argument if stop < 0 or stop+1 is not a valid
position in s.val uppercase : string -> stringval lowercase : string -> stringval capitalize : string -> stringval uncapitalize : string -> stringval uppercase_ascii : string -> stringval lowercase_ascii : string -> stringval capitalize_ascii : string -> stringval uncapitalize_ascii : string -> stringtypet =string
val compare : t -> t -> intPervasives.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.val enum : string -> char BatEnum.t
Examples:
"foo" |> String.enum |> List.of_enum = ['f'; 'o'; 'o']
String.enum "a b c" // ((<>) ' ') |> String.of_enum = "abc"
val of_enum : char BatEnum.t -> string['f'; 'o'; 'o'] |> List.enum |> String.of_enum = "foo"val backwards : string -> char BatEnum.t
Examples:
"foo" |> String.backwards |> String.of_enum = "oof"
let rev s = String.backwards s |> String.of_enum
val of_backwards : char BatEnum.t -> string
Examples:
"foo" |> String.enum |> String.of_backwards = "oof"
"foo" |> String.backwards |> String.of_backwards = "foo"
let rev s = String.enum s |> String.of_backwards
val of_list : char list -> string
Example: ['c'; 'h'; 'a'; 'r'; 's'] |> String.of_list = "chars"
val to_list : string -> char list
Example: String.to_list "string" |> List.interleave ';' |> String.of_list = "s;t;r;i;n;g"
val of_int : int -> string
Example: String.of_int 56 = "56" && String.of_int (-1) = "-1"
val of_float : float -> string
Example: String.of_float 1.246 = "1.246"
val of_char : char -> string
Example: String.of_char 's' = "s"
val to_int : string -> intFailure if the string does not represent an
integer. 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.
Examples: String.to_int "8_480" = String.to_int "0x21_20"
try ignore(String.to_int "2,3"); false with Failure _ -> true
Failure if the string does not represent an integer.val to_float : string -> floatFailure if the string does not represent a float.
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.
Examples: String.to_float "12.34e-1" = String.to_float "1.234"
String.to_float "1" = 1.
try ignore(String.to_float ""); false with Failure _ -> true
Failure if the string does not represent a float.val map : (char -> char) -> string -> stringmap f s returns a string where all characters c in s have been
replaced by f c.
Example: String.map Char.uppercase "Five" = "FIVE"
*
val fold_left : ('a -> char -> 'a) -> 'a -> string -> 'afold_left f a s is
f (... (f (f a s.[0]) s.[1]) ...) s.[n-1]
Examples: String.fold_left (fun li c -> c::li) [] "foo" = ['o';'o';'f']
String.fold_left max 'a' "apples" = 's'
val fold_lefti : ('a -> int -> char -> 'a) -> 'a -> string -> 'afold_left, but with the index of the element as additional argumentval fold_right : (char -> 'a -> 'a) -> string -> 'a -> 'afold_right f s b is
f s.[0] (f s.[1] (... (f s.[n-1] b) ...))
Examples: String.fold_right List.cons "foo" [] = ['f';'o';'o']
String.fold_right (fun c a -> if c = ' ' then a+1 else a) "a b c" 0 = 2
val fold_righti : (int -> char -> 'a -> 'a) -> string -> 'a -> 'afold_right, but with the index of the element as additional argumentval filter : (char -> bool) -> string -> stringfilter f s returns a copy of string s in which only
characters c such that f c = true remain.
Example: String.filter ((<>) ' ') "a b c" = "abc"
val filter_map : (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).
Example: String.filter_map (function 'a'..'z' as c -> Some (Char.uppercase c) | _ -> None) "a b c" = "ABC"
val iteri : (int -> char -> unit) -> string -> unitString.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] ]
val find : string -> string -> intfind s x returns the starting index of the first occurrence of
string x within string s.
Note This implementation is optimized for short strings.
Raises Not_found if x is not a substring of s.
Example: String.find "foobarbaz" "bar" = 3
val find_from : string -> int -> string -> intfind_from s pos x behaves as find s x but starts searching
at position pos. find s x is equivalent to find_from s 0 x.Not_found if not substring is foundInvalid_argument if pos is not a valid position in the string.
Example: String.find_from "foobarbaz" 4 "ba" = 6
val rfind : string -> string -> intrfind s x returns the starting index of the last occurrence
of string x within string s.
Note This implementation is optimized for short strings.
Raises Not_found if x is not a substring of s.
Example: String.rfind "foobarbaz" "ba" = 6
val rfind_from : string -> int -> string -> intrfind_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.
Raises
Not_found if not substring is foundInvalid_argument if pos is not a valid position in the string.
Example: String.rfind_from "foobarbaz" 6 "ba" = 6
val find_all : string -> string -> int BatEnum.tfind_all s x enumerates positions of s at which x occurs.
Example: find_all "aabaabaa" "aba" |> List.of_enum will return
the list [1; 4].val ends_with : string -> string -> boolends_with s x returns true if the string s is ending with x, false otherwise.
Example: String.ends_with "foobarbaz" "rbaz" = true
val starts_with : string -> string -> boolstarts_with s x returns true if s is starting with x, false otherwise.
Example: String.starts_with "foobarbaz" "fooz" = false
val exists : string -> string -> boolexists str sub returns true if sub is a substring of str or
false otherwise.
Example: String.exists "foobarbaz" "obar" = true
val lchop : ?n:int -> string -> stringn characters.
By default n is 1.
If n is strictly less than zeroInvalid_argument. If the string has n or less characters, returns the empty string.
Example:
String.lchop "Weeble" = "eeble"
String.lchop ~n:3 "Weeble" = "ble"
String.lchop ~n:1000 "Weeble" = ""
val rchop : ?n:int -> string -> stringn characters.
By default n is 1.
If n is strictly less than zeroInvalid_argument. If the string has n or less characters , returns the empty string.
Example:
String.rchop "Weeble" = "Weebl"
String.rchop ~n:3 "Weeble" = "Wee"
String.rchop ~n:1000 "Weeble" = ""
val trim : string -> stringBatChar.is_whitespace).
Example: String.trim " \t foo\n " = "foo"
val quote : string -> string
Examples:
String.quote "foo" = "\"foo\""
String.quote "\"foo\"" = "\"\\\"foo\\\"\""
String.quote "\n" = "\"\\n\""
etc.
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.
val left : string -> int -> stringleft r len returns the string containing the len first
characters of r. If r contains less than len characters, it
returns r.
Examples:
String.left "Weeble" 4 = "Weeb"
String.left "Weeble" 0 = ""
String.left "Weeble" 10 = "Weeble"
val right : string -> int -> stringleft r len returns the string containing the len last characters of r.
If r contains less than len characters, it returns r.
Example: String.right "Weeble" 4 = "eble"
val head : string -> int -> string
val tail : string -> int -> stringtail r pos returns the string containing all but the pos first characters of r
Example: String.tail "Weeble" 4 = "le"
val strip : ?chars:string -> string -> string
Examples:
String.strip " foo " = "foo"
String.strip ~chars:" ,()" " boo() bar()" = "boo() bar"
val replace_chars : (char -> string) -> string -> stringreplace_chars f s returns a string where all chars c of s have been
replaced by the string returned by f c.
Example: String.replace_chars (function ' ' -> "(space)" | c -> String.of_char c) "foo bar" = "foo(space)bar"
val replace : str:string -> sub:string -> by:string -> bool * stringreplace ~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 subtitution has taken place.
Example: String.replace "foobarbaz" "bar" "rab" = (true, "foorabbaz")
val nreplace : str:string -> sub:string -> by:string -> stringnreplace ~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.
Example: nreplace ~str:"bar foo aaa bar" ~sub:"aa" ~by:"foo" = "bar foo afoo bar"
val repeat : string -> int -> stringrepeat s n returns s ^ s ^ ... ^ s
Example: String.repeat "foo" 4 = "foofoofoofoo"
val rev : string -> stringstring s returns the reverse of string sval rev_in_place : string -> unitrev_in_place s mutates the string 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".val in_place_mirror : string -> unitString.rev_in_place insteadval split : string -> by:string -> string * stringsplit s sep splits the string s between the first
occurrence of sep, and returns the two parts before
and after the occurence (excluded).Not_found if the separator is not found.
Examples:
String.split "abcabcabc" "bc" = ("a","abcabc")
String.split "abcabcabc" "" = ("","abcabcabc")
val rsplit : string -> by:string -> string * stringrsplit s sep splits the string s between the last occurrence
of sep, and returns the two parts before and after the
occurence (excluded).Not_found if the separator is not found.
Example: String.rsplit "abcabcabc" "bc" = ("abcabca","")
val nsplit : string -> by:string -> string listnsplit s sep splits the string s into a list of strings
which are separated by sep (excluded).
nsplit "" _ returns the empty list.
Example: String.nsplit "abcabcabc" "bc" = ["a"; "a"; "a"; ""]
val join : string -> string list -> stringBatString.concatval slice : ?first:int -> ?last:int -> string -> stringslice ?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 "
val splice : string -> int -> int -> string -> stringString.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"
val explode : string -> char listexplode s returns the list of characters in the string s.
Example: String.explode "foo" = ['f'; 'o'; 'o']
val implode : char list -> stringimplode cs returns a string resulting from concatenating
the characters in the list cs.
Example: String.implode ['b'; 'a'; 'r'] = "bar"
val equal : t -> t -> boolval ord : t -> t -> BatOrd.orderBatOrdval compare : t -> t -> intPervasives.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"
val icompare : t -> t -> int
Example: String.icompare "FOO" "bar" = 1 i.e. "foo" > "bar"
module IString:BatInterfaces.OrderedTypewith type t = t
val numeric_compare : t -> t -> int
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.
Example: String.numeric_compare "xx32" "xx210" < 0
module NumString:BatInterfaces.OrderedTypewith type t = t
val edit_distance : t -> t -> int
val print : 'a BatInnerIO.output -> string -> unit
Example: String.print stdout "foo\n"
val println : 'a BatInnerIO.output -> string -> unit
Example: String.println stdout "foo"
val print_quoted : 'a BatInnerIO.output -> string -> unitquote function.
String.print_quoted stdout "foo" prints "foo" (with the quotes).
String.print_quoted stdout "\"bar\"" prints "\"bar\"" (with the quotes).
String.print_quoted stdout "\n" prints "\n" (not the escaped
character, but '\' then 'n').
module Exceptionless:sig..end
module Cap:sig..end