module Kwstring:sig
..end
exception NoTransformation
val chars : char -> char -> char list
chars lo hi
: generate a list of the characters between lo
and hi
inclusive.val (--) : char -> char -> char list
chars
val rev : string -> string
rev str
: reverse the stringval compare_nocase : string -> string -> int
Faster than fun a b -> String.compare (String.lowercase a) (String.lowercase b)
on long strings that differ near the beginning; slower (about 2x) if they don't
differ or only differ near the end. So, better for random long strings. Tail-recursive.
Returns an int like Pervasives.compare (-, 0, +)
a
: a stringval fold_left : ('a -> char -> 'a) -> 'a -> string -> 'a
fold_left f a str
is f (... (f (f a str.[0]) str.[1]) ...) str.[n-1]
.
Tail-recursive.
Returns whatever f
combines
f
: function f a c
"combines" a
(the result so far) and c
(the current character)a
: identity elementstr
: string
to foldval foldl : ('a -> char -> 'a) -> 'a -> string -> 'a
Kwstring.fold_left
.val fold_right : (char -> 'a -> 'a) -> string -> 'a -> 'a
fold_right f str a
is f str.[0] (f str.[1] (... (f str.[n-1] a) ...))
.
Not tail-recursive.
Returns whatever f
combines
f
: function f c a
"combines" a
(the result so far) and c
(the current character)str
: string
to folda
: identity elementval foldr : (char -> 'a -> 'a) -> string -> 'a -> 'a
Kwstring.fold_right
.val map : (char -> char) -> string -> string
String.map
insteadmap f str
applies f
to each char
in str
, returning a new string of the results.string
f
: function (char -> char)
to transform each characterstr
: a string
val smap : (char -> string) -> string -> string
smap f str
applies f
to each char
in str
, returning a new string of the results.
Unlike map, f
can transform a character into a string, so the length of the resulting
string can be longer than the original. For example, you can HTML-quote a string with:
smap (function | '&' -> "&" | '<' -> "<" | c -> String.make 1 c)
If f
raises NoTransformation
, the character will be mapped into the new string unchanged;
this is more efficient (by about a factor of two) as it saves on string construction in f
.
Hence the example above can be written:
smap (function | '&' -> "&" | '<' -> "<" | _ -> raise NoTransformation)
string
f
: function (char -> string)
to transform each characterstr
: a string
val split : ?_tr:'a -> ?merge:bool -> string -> string -> string list
split ?merge sep str
: split str
on a separator character (any of the characters in sep
)._tr
: DEPRECATEDmerge
: consider runs of consecutive chars from sep
to be one
separator; so ~merge:false
is good for /etc/passwd while ~merge:true
is good for lexing "words" from textsep
: string interpreted as set of characters to split onval wsplit : ?merge:bool -> string -> string list
wsplit ?merge str
: shorthand for split " \t\n\r"
.val whitespace : string
trimleft
, trimright
and trim
.val trimleft : string -> string -> string
Not tail recursive.
Returns trimmed string
cs
: string of characters to trim, treated as a setstr
: string to be trimmedval trimright : string -> string -> string
Tail recursive.
Returns trimmed string
cs
: string of characters to trim, treated as a setstr
: string to be trimmedval trim : string -> string -> string
cs
: string of characters to trim, treated as a setstr
: string to be trimmedval trimto : int -> string -> string
Returns a new string.
max
: maximum length of stringstr
: the string to trimval from : int -> string -> string
start
: index of first desired character in string; will be first character in resultstr
: the stringval contains : string -> string -> bool
contains cset str
: test whether any of the characters in cset
are contained in str
.
Like flip String.contains
except cset
is a string interpreted
as a set of characters.
cset
: a string interpreted as a set of charactersstr
: string of characters to examineval skipwhile : string -> string -> string
Partially apply for efficiency.
Returns string without leading characters
cs
: string of characters (interpreted as a set) to skipval splitwhile : string -> string -> string * string
cs
, second is remainder of string
Partially apply for efficiency.
Returns pair of leading component, rest of string
cs
: string of characters (interpreted as a set) making up leading componentval splitat : int -> string -> string * string
splitat n str
: given an integer and a string, splits the string
into two strings (returned as a tuple) at the position
corresponding to the given integer.
If n < 0
, take it as an index from the end of the string. So
splitat ~-1 "foo" = ("fo", "o")
.
If the integer is greater than the length of the string, it returns
a tuple containing the entire string as its first element and the
empty string as its second element.
Returns the two parts of the string
n
: the split positionstr
: the stringval prefix : string -> string -> bool
prefix pre str
: is pre
a prefix of str
?type
side =
| |
Left |
| |
Right |
| |
Both |
pad
's ~side
paramaterval pad : ?c:char -> ?trunc:bool -> ?side:side -> int -> string -> string
pad ?c ?trunc ?side n s
: pad a string out to some size.
With ~side:Both
, can also be thought of as centering text.
Returns padded string with length >= n
(trunc = false
) or length = n
(trunc = true
)
c
: character to pad with (default: ' ' (space))trunc
: whether to truncate string longer than n
to n
chars (default: false
)side
: which side of string tp pad: Left, Right or Both; also determines which side
gets truncated (default: Right
)n
: desired length of strings
: string to padval repeat : char -> int -> string
repeat c n
: return a string consisting of the character c
repeated n
times.c
: character to repeatn
: desired length of stringval base2 : ?width:int -> int -> string
n
to binary (base-2) string representationwidth
: width of string in characters (default: use minium number of chars)n
: integer to convertval of_chars : ?len:int -> char list -> string
of_chars ?len list
: turn list of chars into a stringlen
: size of internal buffer (set to List.length list
for greatest efficiency)list
: the listval untabify : ?tc:char -> ?stop:int -> string -> string
untabify ?tc ?stop str
: convert all tabs in str
to multiple spaces,
preserving columns.tc
: the tabulation character (default: '\t'
)stop
: the tab stop (default: 8
)str
: the string to untabify