Module Stdlib320.String

Basic functions

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

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

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

length s is the number of characters in s, e.g., length "ABC" = 3.

val get : string -> int -> char

Gets the character in a given string at a given index. get s i is the ith character of s if i >= 0 and i < length s. It is equivalent to writing s.[i].

Raises an Invalid_argument exception otherwise.

val concat : string -> string list -> string

Combines a list of string with a given delimiter, e.g., concat "," ["A";"B";"C"] = "A,B,C"

Higher-order functions

These work the same as they would on lists of char, e.g., map f s = List.map f (to_list s).

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

Modifying

val trim : string -> string

Removes whitespace from the beginning and end of its argument.

val uppercase_ascii : string -> string

Equivalent to map Char.upper_case_ascii

val lowercase_ascii : string -> string

Equivalent to map Char.lower_case_ascii

Indexing

val index : string -> char -> int

index s c is the index of the first occurrence of c in s.

If c does not appear in s, then it raises an Invalid_argument expection.

val index_opt : string -> char -> int option

Same as the above function, but is None in the case of failure.

val to_list : string -> char list

Converts a string to a list of characters, e.g., to_list "ABC" = ['A';'B';'C']

val of_list : char list -> string

Converts a list of characters to a string, e.g., of_list ['A';'B';'C'] = "ABC"