Module Stdlib320.String

A module containing basic string operations

Basic functions

val init : int -> (int -> char) -> string

Creates a string based on an index function, i.e., init n f is a string of length n whose ith character is f i

let _ = assert (init 3 (fun x -> char_of_int (x + 65)) = "ABC")
let _ = assert (init 5 (fun _ -> 'z') = "zzzzz")
val length : string -> int

length s is the number of characters in s

let _ = assert (length "ABC" = 3)
val get : string -> int -> char

Gets the character in a string at a given index, i.e., get s i is the ith character of s. It's equivalent to writing s.[i]

Raises an Invalid_argument exception if i < 0 and i >= length s

val concat : string -> string list -> string

Combines a list of string with a given delimiter

let _ = assert (concat "," ["A";"B";"C"] = "A,B,C")
let _ = assert (concat "" ["1";"2";"3"] = "123")
val trim : string -> string

Removes whitespace from the beginning and end of its argument

let _ = assert ("  xyz  \n" = "xyz")
val sub : string -> int -> int -> string

Gets a substring, i.e., sub s p l is the substring of s of length l starting at position p, where a position is a value between 0 and length s which marks the beginning of a substring

let _ = assert (sub "ABC" 0 2= "AB")
let _ = assert (sub "ABC" 3 0 = "")
let _ = assert (sub "ABC" 1 1 = "B")

Raises an Invalid_argument exception if p < 0 or p + l > length s

Higher-order functions

These functions work the same as they would on a char list.

val map : (char -> char) -> string -> string
val fold_left : ('acc -> char -> 'acc) -> 'acc -> string -> 'acc
val fold_right : (char -> 'acc -> 'acc) -> string -> 'acc -> 'acc