Module BatString


module BatString: sig .. end
String operations.

This module extends Stdlib's String module, go there for documentation on the rest of the functions and types.

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 is_empty : string -> bool
is_empty s returns true if s is the empty string, false otherwise.

Usually a tad faster than comparing s with "".

Example: if String.is_empty s then "(Empty)" else s

val init : int -> (int -> char) -> string
init 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 enum : string -> char BatEnum.t
Conversions

Returns an enumeration of the characters of a string.

Example: String.enum se // (fun c -> not (is_bad_char c)) |> String.of_enum

val of_enum : char BatEnum.t -> string
Creates a string from a character enumeration.

Example: String.enum se // (fun c -> not (is_bad_char c)) |> String.of_enum

val backwards : string -> char BatEnum.t
Returns an enumeration of the characters of a string, from last to first.

Example: let rev s = String.backwards s |> String.of_enum

val of_backwards : char BatEnum.t -> string
Build a string from an enumeration, starting with last character, ending with first.

Example: let rev s = String.enum s |> String.of_backwards

val of_list : char list -> string
Converts a list of characters to a string.

Example: ['c'; 'h'; 'a'; 'r'; 's'] |> String.of_list

val to_list : string -> char list
Converts a string to the list of its characters.

Example: String.to_list "string" |> List.interleave ';' |> String.of_list = "s;t;r;i;n;g"

val of_int : int -> string
Returns the string representation of an int.

Example: String.of_int 56 = "56"

val of_float : float -> string
Returns the string representation of an float.

Example: String.of_float 1.246 = "1.246"

val of_char : char -> string
Returns a string containing one given character.

Example: String.of_char 's' = "s"

val to_int : string -> int
Returns the integer represented by the given string or raises Invalid_string 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.

Example: String.to_int "8_480" = String.to_int "0x21_20"

val to_float : string -> float
Returns the float represented by the given string or raises Invalid_string 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.

Example: String.of_float "12.34e-1" = String.of_float "1.234"


String traversals

val map : (char -> char) -> string -> string
map 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 -> 'a
fold_left f a s is f (... (f (f a s.[0]) s.[1]) ...) s.[n-1]

Example: String.fold_left max "apples" = 's'

val fold_right : (char -> 'a -> 'a) -> string -> 'a -> 'a
fold_right f s b is f s.[0] (f s.[1] (... (f s.[n-1] b) ...))

Example: String.fold_right (fun a c -> if c = ' ' then a+1 else a) 0 s

val filter : (char -> bool) -> string -> string
filter f s returns a copy of string s in which only characters c such that f c = true remain.

Example: String.filter is_bad_char s

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).

Example: String.filter_map (function 'a'-'z' as c -> Some c | _ -> None)

val 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 pos = Array.make 256 0 in String.iteri (fun i c -> pos.(int_of_char c) <- i


Finding

val find : string -> string -> int
find 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 Invalid_string if x is not a substring of s.

Example: String.find "foobarbaz" "bar" = 3

val find_from : string -> int -> string -> int
find_from s ofs x behaves as find s x but starts searching at offset ofs. find s x is equivalent to find_from s 0 x.

Example: String.find_from "foobarbaz" 4 "ba" = 6

val rfind : string -> string -> int
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.
Raises Invalid_string if x is not a substring of s.

Example: String.rfind "foobarbaz" "ba" = 6

val rfind_from : string -> int -> string -> int
rfind_from s ofs x behaves as rfind s x but starts searching at offset ofs. rfind s x is equivalent to rfind_from s (String.length s - 1) x.

Example: String.rfind_from "foobarbaz" 6 "ba" = 6

val ends_with : string -> string -> bool
ends_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 -> bool
starts_with s x returns true if s is starting with x, false otherwise.

Example: String.starts_with "foobarbaz" "fooz" = false

val exists : string -> string -> bool
exists str sub returns true if sub is a substring of str or false otherwise.

Example: String.exists "foobarbaz" "obar" = true


Transformations

val lchop : string -> string
Returns the same string but without the first character. does nothing if the string is empty.

Example: String.lchop "Weeble" = "eeble"

val rchop : string -> string
Returns the same string but without the last character. does nothing if the string is empty.

Example: String.rchop "Weeble" = "Weebl"

val trim : string -> string
Returns the same string but without the leading and trailing whitespaces.

Example: String.trim " \t foo " = "foo"

val quote : string -> string
Add quotes around a string and escape any quote appearing in that string. This function is used typically when you need to generate source code from a string.

Examples: quote "foo" returns "\"foo\"" quote "\"foo\"" returns "\\\"foo\\\"" etc.

val left : string -> int -> string
left r len returns the string containing the len first characters of r

Example: String.left "Weeble" 4 = "Weeb"

val right : string -> int -> string
left r len returns the string containing the len last characters of r

Example: String.right "Weeble" 4 = "eble"

val head : string -> int -> string
as BatString.left
val tail : string -> int -> string
tail 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
Returns the string without the chars if they are at the beginning or at the end of the string. By default chars are " \t\r\n".

Example: String.strip ~chars:" ,()" " boo() bar()" = "boo() bar"

val replace_chars : (char -> string) -> string -> string
replace_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 -> c) "foo bar" = "foo(space)bar"

val replace : str:string -> sub:string -> by:string -> bool * string
replace ~str ~sub ~by returns a tuple constisting 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 repeat : string -> int -> string
repeat s n returns s ^ s ^ ... ^ s

Example: String.repeat "foo" 4 = "foofoofoofoo"


Splitting around

val split : string -> string -> string * string
split s sep splits the string s between the first occurrence of sep.
Raises Invalid_string if the separator is not found.

Example: String.split "abcabcabc" "bc" = ("a","abcabc")

val rsplit : string -> string -> string * string
rsplit s sep splits the string s between the last occurrence of sep.
Raises Invalid_string if the separator is not found.

Example: String.rsplit "abcabcabc" "bc" = ("abcabca","")

val nsplit : string -> string -> string list
nsplit s sep splits the string s into a list of strings which are separated by sep. nsplit "" _ returns the empty list.

Example: String.nsplit "abcabcabc" "bc" = ["a"; "a"; "a"; ""]

val join : string -> string list -> string
Same as concat
val slice : ?first:int -> ?last:int -> string -> string
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 1 (-3) " foo bar baz" = "foo bar "

val splice : string -> int -> int -> string -> string
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.

Example: String.splice "foo bar baz" 3 5 "XXX" = "fooXXXbaz"

val explode : string -> char list
explode s returns the list of characters in the string s.

Example: String.explode "foo" = ['f'; 'o'; 'o']

val implode : char list -> string
implode cs returns a string resulting from concatenating the characters in the list cs.

Example: String.implode ['b'; 'a'; 'r'] = "bar"


Comparisons

val compare : String.t -> String.t -> int
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"

val icompare : String.t -> String.t -> int
Compare two strings, case-insensitive.

Example: String.icompare "FOO" "bar" = 1 i.e. "foo" > "bar"

module IString: BatInterfaces.OrderedType  with type t = t
uses icompare as ordering function
val numeric_compare : String.t -> String.t -> int
Compare two strings, sorting "abc32def" before "abc210abc".

Algorithm: Ignore identical prefixes, if first character difference is numeric, parse the whole number as an int and compare.

Example: String.numeric_compare "xx32" "xx210" = -1

module NumString: BatInterfaces.OrderedType  with type t = t
uses numeric_compare as its ordering function

Boilerplate code


Printing

val print : 'a BatInnerIO.output -> string -> unit
Print a string.

Example: String.print stdout "foo\n"

val println : 'a BatInnerIO.output -> string -> unit
Print a string, end the line.

Example: String.println stdout "foo"

val print_quoted : 'a BatInnerIO.output -> string -> unit
Print a string, with quotes.

print_quoted stdout "foo" prints "foo" (with the quotes)

print_quoted stdout "\"bar\"" prints "\"bar\"" (with the quotes)

val t_printer : String.t BatValue_printer.t