Module Tablecloth.String
Functions for working with "strings"
Functions for working with "strings"
Create
Strings literals are created with the "double quotes" syntax.
val fromChar : char -> stringConverts the given character to an equivalent string of length one.
val from_char : char -> stringval fromArray : char array -> stringCreate 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 : char array -> stringval fromList : char list -> stringCreate 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 : char list -> stringval repeat : string -> count:int -> stringCreate 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 : int -> f:(int -> char) -> stringCreate 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 : string -> int -> charGet the character at the specified index
val getAt : string -> index:int -> char optionGet the character at ~index
val get_at : string -> index:int -> char optionval (.?[]) : string -> int -> char optionThe 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 : string -> stringReverse a string
Note This function does not work with Unicode characters.
Examples
String.reverse "stressed" = "desserts"
val slice : ?to_:int -> string -> from:int -> stringExtract a substring from the specified indicies.
See Array.slice.
Query
val isEmpty : string -> boolCheck if a string is empty
val is_empty : string -> boolval length : string -> intReturns 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 : string -> prefix:string -> boolSee if the second string starts with prefix
Examples
String.startsWith ~prefix:"the" "theory" = true
String.startsWith ~prefix:"ory" "theory" = false
val starts_with : string -> prefix:string -> boolval endsWith : string -> suffix:string -> boolSee if the second string ends with suffix.
Examples
String.endsWith ~suffix:"the" "theory" = false
String.endsWith ~suffix:"ory" "theory" = true
val ends_with : string -> suffix:string -> boolval includes : string -> substring:string -> boolCheck 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 : string -> boolTest 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 : string -> boolModify
val dropLeft : string -> count:int -> stringDrop 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 : string -> count:int -> stringval dropRight : string -> count:int -> stringDrop 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 : string -> count:int -> stringval indexOf : string -> string -> int optionReturns 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 : string -> string -> int optionval indexOfRight : string -> string -> int optionReturns 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 : string -> string -> int optionval insertAt : string -> index:int -> value:t -> stringInsert 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 : string -> index:int -> value:t -> stringval toLowercase : string -> stringConverts 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 : string -> stringval toUppercase : string -> stringConverts 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 : string -> stringval uncapitalize : string -> stringConverts 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 : string -> stringConverts 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 : string -> stringRemoves 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 : string -> stringLike trim but only drops characters from the beginning of the string.
val trim_left : string -> stringval trim_right : string -> stringLike trim but only drops characters from the end of the string.
val padLeft : string -> int -> with_:string -> stringPad 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 : string -> int -> with_:string -> stringval padRight : string -> int -> with_:string -> stringPad 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 : string -> int -> with_:string -> stringDeconstruct
val uncons : string -> (char * string) optionReturns, 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 : string -> on:string -> string listDivide 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 : string -> f:(char -> unit) -> unitRun f on each character in a string.
val for_each : string -> f:(char -> unit) -> unitval fold : string -> initial:'a -> f:('a -> char -> 'a) -> 'aConvert
val toArray : string -> char arrayReturns an Array of the individual characters in the given string.
Examples
String.toArray "" = [||]
String.toArray "abc" = [|'a'; 'b'; 'c'|]
val to_array : string -> char arrayval toList : string -> char listReturns a List of the individual characters in the given string.
Examples
String.toList "" = []
String.toList "abc" = ['a'; 'b'; 'c']
val to_list : string -> char listCompare
val equal : string -> string -> boolTest two string for equality
val compare : string -> string -> intCompare 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
val comparator : (t, identity) Tablecloth__.TableclothComparator.t