Module TableclothString
Functions for working with "strings"
type t
type t = string
Create
Strings literals are created with the "double quotes" syntax.
val fromChar
val fromChar : char -> string
Converts the given character to an equivalent string of length one.
val from_char
val from_char : char -> string
val fromArray
val fromArray : char array -> string
Create a string from an Array of characters.
Note that these must be individual characters in single quotes, not strings of length one.
Examples
String.fromArray [||] = ""
String.fromArray [|'a'; 'b'; 'c'|] = "abc"
val from_array
val from_array : char array -> string
val fromList
val fromList : char list -> string
Create a string from a List of characters.
Note that these must be individual characters in single quotes, not strings of length one.
Examples
String.fromList [] = ""
String.fromList ['a'; 'b'; 'c'] = "abc"
val from_list
val from_list : char list -> string
val repeat
val repeat : string -> count:int -> string
Create a string by repeating a string count time.
Exceptions
If count is negative, String.repeat throws a RangeError exception.
Examples
String.repeat ~count:3 "ok" = "okokok"
String.repeat ~count:3 "" = ""
String.repeat ~count:0 "ok" = ""
val initialize
val initialize : int -> f:( int -> char ) -> string
Create a string by providing a length and a function to choose characters.
Returns an empty string if the length is negative.
Examples
String.initialize 8 ~f:(Fun.constant '9') = "999999999"
Basic operations
val get
val get : string -> int -> char
Get the character at the specified index
val getAt
val getAt : string -> index:int -> char option
Get the character at ~index
val get_at
val get_at : string -> index:int -> char option
val (.?[])
val (.?[]) : string -> int -> char option
The index
operator
version of getAt
Note Currently this is only supported by the OCaml syntax.
Examples
("Doggie".String.?[3]) = Some 'g'
String.("Doggie".?[9]) = None
val reverse
val reverse : string -> string
Reverse a string
Note This function does not work with Unicode characters.
Examples
String.reverse "stressed" = "desserts"
val slice
val slice : ?to_:int -> string -> from:int -> string
Extract a substring from the specified indicies.
See Array.slice.
Query
val isEmpty
val isEmpty : string -> bool
Check if a string is empty
val is_empty
val is_empty : string -> bool
val length
val length : string -> int
Returns the length of the given string.
Warning if the string contains non-ASCII characters then length
will not equal the number of characters
Examples
String.length "abc" = 3
val startsWith
val startsWith : string -> prefix:string -> bool
See if the second string starts with prefix
Examples
String.startsWith ~prefix:"the" "theory" = true
String.startsWith ~prefix:"ory" "theory" = false
val starts_with
val starts_with : string -> prefix:string -> bool
val endsWith
val endsWith : string -> suffix:string -> bool
See if the second string ends with suffix.
Examples
String.endsWith ~suffix:"the" "theory" = false
String.endsWith ~suffix:"ory" "theory" = true
val ends_with
val ends_with : string -> suffix:string -> bool
val includes
val includes : string -> substring:string -> bool
Check if one string appears within another
Examples
String.includes "team" ~substring:"tea" = true
String.includes "team" ~substring:"i" = false
String.includes "ABC" ~substring:"" = true
val isCapitalized
val isCapitalized : string -> bool
Test if the first letter of a string is upper case.
Note This function works only with ASCII characters, not Unicode.
Examples
String.isCapitalized "Anastasia" = true
String.isCapitalized "" = false
val is_capitalized
val is_capitalized : string -> bool
Modify
val dropLeft
val dropLeft : string -> count:int -> string
Drop count characters from the left side of a string.
Examples
String.dropLeft ~count:3 "abcdefg" = "defg"
String.dropLeft ~count:0 "abcdefg" = "abcdefg"
String.dropLeft ~count:7 "abcdefg" = ""
String.dropLeft ~count:(-2) "abcdefg" = "fg"
String.dropLeft ~count:8 "abcdefg" = ""
val drop_left
val drop_left : string -> count:int -> string
val dropRight
val dropRight : string -> count:int -> string
Drop count characters from the right side of a string.
Examples
String.dropRight ~count:3 "abcdefg" = "abcd"
String.dropRight ~count:0 "abcdefg" = "abcdefg"
String.dropRight ~count:7 "abcdefg" = ""
String.dropRight ~count:(-2) "abcdefg" = "abcdefg"
String.dropRight ~count:8 "abcdefg" = ""
val drop_right
val drop_right : string -> count:int -> string
val indexOf
val indexOf : string -> string -> int option
Returns the index of the first occurrence of string or None if string
has no occurences of string
Examples
String.indexOf "Hello World World" "World" = Some 6
String.indexOf "Hello World World" "Bye" = None
val index_of
val index_of : string -> string -> int option
val indexOfRight
val indexOfRight : string -> string -> int option
Returns the index of the last occurrence of string or None if string
has no occurences of string
Examples
String.indexOfRight "Hello World World" "World" = Some 12
String.indexOfRight "Hello World World" "Bye" = None
val index_of_right
val index_of_right : string -> string -> int option
val insertAt
val insertAt : string -> index:int -> value:t -> string
Insert a string at index.
The character previously at index will now follow the inserted string.
Examples
String.insertAt ~value:"**" ~index:2 "abcde" = "ab**cde"
String.insertAt ~value:"**" ~index:0 "abcde" = "**abcde"
String.insertAt ~value:"**" ~index:5 "abcde" = "abcde**"
String.insertAt ~value:"**" ~index:(-2) "abcde" = "abc**de"
String.insertAt ~value:"**" ~index:(-9) "abcde" = "**abcde"
String.insertAt ~value:"**" ~index:9 "abcde" = "abcde**"
val insert_at
val insert_at : string -> index:int -> value:t -> string
val toLowercase
val toLowercase : string -> string
Converts all upper case letters to lower case.
Note This function works only with ASCII characters, not Unicode.
Examples
String.toLowercase "AaBbCc123" = "aabbcc123"
val to_lowercase
val to_lowercase : string -> string
val toUppercase
val toUppercase : string -> string
Converts all lower case letters to upper case.
Note This function works only with ASCII characters, not Unicode.
Examples
String.toUppercase "AaBbCc123" = "AABBCC123"
val to_uppercase
val to_uppercase : string -> string
val uncapitalize
val uncapitalize : string -> string
Converts the first letter to lower case if it is upper case.
Note This function works only with ASCII characters, not Unicode.
Examples
String.uncapitalize "Anastasia" = "anastasia"
val capitalize
val capitalize : string -> string
Converts the first letter of s to lowercase if it is upper case.
Note This function works only with ASCII characters, not Unicode.
Examples
String.uncapitalize "den" = "Den"
val trim
val trim : string -> string
Removes leading and trailing whitespace from a string
Examples
String.trim " abc " = "abc"
String.trim " abc def " = "abc def"
String.trim "\r\n\t abc \n\n" = "abc"
val trimLeft
val trimLeft : string -> string
Like trim but only drops characters from the beginning of
the string.
val trim_left
val trim_left : string -> string
val trim_right
val trim_right : string -> string
Like trim but only drops characters from the end of the
string.
val padLeft
val padLeft : string -> int -> with_:string -> string
Pad a string up to a minimum length
If the string is shorted than the proivded length, adds with to the
left of the string until the minimum length is met
Examples
String.padLeft "5" 3 ~with_:"0" = "005"
val pad_left
val pad_left : string -> int -> with_:string -> string
val padRight
val padRight : string -> int -> with_:string -> string
Pad a string up to a minimum length
If the string is shorted than the proivded length, adds with to the
left of the string until the minimum length is met
Examples
String.padRight "Ahh" 7 ~with_:"h" = "Ahhhhhh"
val pad_right
val pad_right : string -> int -> with_:string -> string
Deconstruct
val uncons
val uncons : string -> (char * string) option
Returns, as an Option, a tuple containing the first Char and the
remaining String.
If given an empty string, returns None.
Examples
String.uncons "abcde" = Some ('a', "bcde")
String.uncons "a" = Some ('a', "")
String.uncons "" = None
val split
val split : string -> on:string -> string list
Divide a string into a list of strings, splitting whenever on is
encountered.
Examples
String.split ~on:"/" "a/b/c" = ["a"; "b"; "c"]
String.split ~on:"--" "a--b--c" = ["a"; "b"; "c"]
String.split ~on:"/" "abc" = ["abc"]
String.split ~on:"/" "" = [""]
String.split ~on:"" "abc" = ["a"; "b"; "c"]
Iterate
val forEach
val forEach : string -> f:( char -> unit ) -> unit
Run f on each character in a string.
val for_each
val for_each : string -> f:( char -> unit ) -> unit
val fold
val fold : string -> initial:'a -> f:( 'a -> char -> 'a ) -> 'a
Like Array.fold but the elements are Chars
Convert
val toArray
val toArray : string -> char array
Returns an Array of the individual characters in the given string.
Examples
String.toArray "" = [||]
String.toArray "abc" = [|'a'; 'b'; 'c'|]
val to_array
val to_array : string -> char array
val toList
val toList : string -> char list
Returns a List of the individual characters in the given string.
Examples
String.toList "" = []
String.toList "abc" = ['a'; 'b'; 'c']
val to_list
val to_list : string -> char list
Compare
val equal
val equal : string -> string -> bool
Test two string for equality
val compare
val compare : string -> string -> int
Compare two strings. Strings use 'dictionary' ordering. 1 Also known as lexicographical ordering.
Examples
String.compare "Z" "A" = 1
String.compare "Be" "Bee" = -1
String.compare "Pear" "pear" = 1
String.compare "Peach" "Peach" = 0
type identity
type identity
The unique identity for Comparator
val comparator
val comparator : ( t, identity ) TableclothComparator.t