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.from_array [||] = ""
String.from_array [|'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.from_list [] = ""
String.from_list ['a'; 'b'; 'c'] = "abc"
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" = ""
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') = "99999999"
Get the character at the specified index
Exceptions
If index out of range, throws a Invalid_argument
exception. Concider using getAt
, it returns an option<char>
Examples
String.get "stressed" 1 = 't'
The index operator version of get_at
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 string starts with prefix
.
Examples
String.starts_with ~prefix:"the" "theory" = true
String.starts_with ~prefix:"ory" "theory" = false
See if the string ends with suffix
.
Examples
String.ends_with ~suffix:"the" "theory" = false
String.ends_with ~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.is_capitalized "Anastasia" = true
String.is_capitalized "" = false
Drop count
characters from the left side of a string.
Examples
String.drop_left ~count:3 "abcdefg" = "defg"
String.drop_left ~count:0 "abcdefg" = "abcdefg"
String.drop_left ~count:7 "abcdefg" = ""
String.drop_left ~count:(-2) "abcdefg" = "fg"
String.drop_left ~count:8 "abcdefg" = ""
Drop count
characters from the right side of a string.
Examples
String.drop_right ~count:3 "abcdefg" = "abcd"
String.drop_right ~count:0 "abcdefg" = "abcdefg"
String.drop_right ~count:7 "abcdefg" = ""
String.drop_right ~count:(-2) "abcdefg" = "abcdefg"
String.drop_right ~count:8 "abcdefg" = ""
Returns the index of the first occurrence of string
or None if string has no occurences of string
Examples
String.index_of "Hello World World" "World" = Some 6
String.index_of "Hello World World" "Bye" = None
Returns the index of the last occurrence of string
or None if string has no occurences of string
Examples
String.index_of_right "Hello World World" "World" = Some 12
String.index_of_right "Hello World World" "Bye" = None
val insert_at : string -> index:int -> value:t -> string
Insert a string at index
.
The character previously at index will now follow the inserted string.
Examples
String.insert_at ~value:"**" ~index:2 "abcde" = "ab**cde"
String.insert_at ~value:"**" ~index:0 "abcde" = "**abcde"
String.insert_at ~value:"**" ~index:5 "abcde" = "abcde**"
String.insert_at ~value:"**" ~index:(-2) "abcde" = "abc**de"
String.insert_at ~value:"**" ~index:(-9) "abcde" = "**abcde"
String.insert_at ~value:"**" ~index:9 "abcde" = "abcde**"
Converts all upper case letters to lower case.
Note This function works only with ASCII characters, not Unicode.
Examples
String.to_lowercase "AaBbCc123" = "aabbcc123"
Converts all lower case letters to upper case.
Note This function works only with ASCII characters, not Unicode.
Examples
String.to_uppercase "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.pad_left "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.pad_right "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.to_array "" = [||]
String.to_array "abc" = [|'a'; 'b'; 'c'|]
Returns a List
of the individual characters in the given string.
Examples
String.to_list "" = []
String.to_list "abc" = ['a'; 'b'; 'c']
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
The unique identity for Comparator
val comparator : (t, identity) Base.Comparator.t