module String: Extlib.ExtString.String
String operations.exception Invalid_string
typet =
string
val length : string -> int
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 ""
.
val get : string -> int -> char
String.get s n
returns character number n
in string s
.
The first character is character number 0.
The last character is character number String.length s - 1
.
You can also write s.[n]
instead of String.get s n
.
Raise Invalid_argument "index out of bounds"
if n
is outside the range 0 to (String.length s - 1)
.
val set : string -> 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
.
Raise Invalid_argument "index out of bounds"
if n
is outside the range 0 to (String.length s - 1)
.val create : int -> string
String.create n
returns a fresh string of length n
.
The string initially contains arbitrary characters.
Raise Invalid_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
.
Raise Invalid_argument
if n < 0
or n >
Sys.max_string_length
.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).val enum : string -> char Enum.t
val of_enum : char Enum.t -> string
val backwards : string -> char Enum.t
val of_backwards : char Enum.t -> string
val of_list : char list -> string
val to_list : string -> char list
val of_int : int -> string
val of_float : float -> string
val of_char : char -> string
val to_int : string -> int
Invalid_string
if the string does not represent an integer.val to_float : string -> float
val map : (char -> char) -> string -> string
map f s
returns a string where all characters c
in s
have been
replaced by f c
. *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]
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) ...))
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.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).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]; ()
.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
.val index : string -> char -> int
String.index s c
returns the position of the leftmost
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 position of the rightmost
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
, but start
searching at the character position given as second argument.
String.index s c
is equivalent to String.index_from s 0 c
.val rindex_from : string -> int -> char -> int
String.rindex
, but start
searching at the character position given as second argument.
String.rindex s c
is equivalent to
String.rindex_from s (String.length s - 1) c
.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
the substring of s
starting from start
to the end of s
.Invalid_argument
if start
is not a valid index of s
.val rcontains_from : string -> int -> char -> bool
String.rcontains_from s stop c
tests if character c
appears in the substring of s
starting from the beginning
of s
to index stop
.Invalid_argument
if stop
is not a valid index of s
.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
.
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
.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
.
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
.val ends_with : string -> string -> bool
ends_with s x
returns true
if the string s
is ending with x
, false
otherwise.val starts_with : string -> string -> bool
starts_with s x
returns true
if s
is starting with x
, false
otherwise.val exists : string -> string -> bool
exists str sub
returns true if sub
is a substring of str
or
false otherwise.val lchop : string -> string
val rchop : string -> string
val trim : string -> string
val quote : string -> string
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
val right : string -> int -> string
left r len
returns the string containing the len
last characters of r
val head : string -> int -> string
String.left
val tail : string -> int -> string
tail r pos
returns the string containing all but the pos
first characters of r
val strip : ?chars:string -> string -> string
val uppercase : string -> string
val lowercase : string -> string
val capitalize : string -> string
val uncapitalize : string -> string
val copy : string -> string
val sub : string -> int -> int -> string
String.sub s start len
returns a fresh string of length len
,
containing the characters number start
to start + len - 1
of string s
.Invalid_argument
if start
and len
do not
designate a valid substring of s
; that is, if start < 0
,
or len < 0
, or start + len >
String.length
s
.val fill : string -> int -> int -> char -> unit
String.fill s start len c
modifies string s
in place,
replacing the characters number start
to start + len - 1
by c
.Invalid_argument
if start
and len
do not
designate a valid substring of s
.val blit : string -> int -> string -> int -> int -> unit
String.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 chunks 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 -> string
String.concat sep sl
concatenates the list of strings sl
,
inserting the separator string sep
between each.val escaped : string -> string
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
.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.val repeat : string -> int -> string
repeat s n
returns s ^ s ^ ... ^ s
val split : string -> string -> string * string
split s sep
splits the string s
between the first
occurrence of sep
.Invalid_string
if the separator is not found.val rsplit : string -> string -> string * string
rsplit s sep
splits the string s
between the last
occurrence of sep
.Invalid_string
if the separator is not found.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.val join : string -> string list -> string
String.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.
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
.
val explode : string -> char list
explode s
returns the list of characters in the string s
.val implode : char list -> string
implode cs
returns a string resulting from concatenating
the characters in the list cs
.val compare : t -> t -> int
Standard.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 icompare : t -> t -> int
module String.IString:Extlib.Interfaces.OrderedType
with type t = t
val numeric_compare : t -> t -> int
module String.NumString:Extlib.Interfaces.OrderedType
with type t = t
val t_of_sexp : Sexplib.Sexp.t -> t
val sexp_of_t : t -> Sexplib.Sexp.t
val print : 'a Extlib.InnerIO.output -> string -> unit
val println : 'a Extlib.InnerIO.output -> string -> unit
val print_quoted : 'a Extlib.InnerIO.output -> string -> unit
print_quoted stdout "foo"
prints "foo"
(with the quotes)
print_quoted stdout "\"bar\""
prints "\"bar\""
(with the quotes)
val t_printer : t Value_printer.t
module String.Cap:sig
..end