Library
Module
Module type
Parameter
Class
Class type
Functions for working with "strings"
Functions for working with "strings"
Strings literals are created with the "double quotes"
syntax.
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.ofArray [||] = ""
String.ofArray [|'a'; 'b'; 'c'|] = "abc"
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.ofList [] = ""
String.ofList ['a'; 'b'; 'c'] = "abc"
Create a string by repeating a string count
times.
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" = ""
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"
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
Reverse a string
Note This function does not work with Unicode characters.
Examples
String.reverse "stressed" = "desserts"
Extract a substring from the specified indicies.
See Array.slice
.
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
See if the second string starts with prefix
Examples
String.startsWith ~prefix:"the" "theory" = true
String.startsWith ~prefix:"ory" "theory" = false
See if the second string ends with suffix
.
Examples
String.endsWith ~suffix:"the" "theory" = false
String.endsWith ~suffix:"ory" "theory" = true
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
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
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" = ""
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 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 ~insert:"**" ~index:2 "abcde" = "ab**cde"
String.insertAt ~insert:"**" ~index:0 "abcde" = "**abcde"
String.insertAt ~insert:"**" ~index:5 "abcde" = "abcde**"
String.insertAt ~insert:"**" ~index:(-2) "abcde" = "abc**de"
String.insertAt ~insert:"**" ~index:(-9) "abcde" = "**abcde"
String.insertAt ~insert:"**" ~index:9 "abcde" = "abcde**"
Converts all upper case letters to lower case.
Note This function works only with ASCII characters, not Unicode.
Examples
String.toLowercase "AaBbCc123" = "aabbcc123"
Converts all lower case letters to upper case.
Note This function works only with ASCII characters, not Unicode.
Examples
String.toUppercase "AaBbCc123" = "AABBCC123"
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"
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.capitalize "den" = "Den"
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"
Like trim
but only drops characters from the beginning of the string.
Like trim
but only drops characters from the end of the 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"
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"
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"]
Like Array.fold
but the elements are Char
s
Returns an Array
of the individual characters in the given string.
Examples
String.toArray "" = [||]
String.toArray "abc" = [|'a'; 'b'; 'c'|]
Returns a List
of the individual characters in the given string.
Examples
String.toList "" = []
String.toList "abc" = ['a'; 'b'; 'c']
The unique identity for Comparator