package reason-standard

  1. Overview
  2. Docs

Functions for working with "strings"

Functions for working with "strings"

type t = string

Create

Strings literals are created with the "double quotes" syntax.

val ofChar : char -> string

Converts the given character to an equivalent string of length one.

val ofArray : 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.ofArray [||] = ""
String.ofArray [|'a'; 'b'; 'c'|] = "abc"
val ofList : 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.ofList [] = ""
String.ofList ['a'; 'b'; 'c'] = "abc"
val repeat : string -> count:int -> string

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" = ""
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 : string -> int -> char

Get the character at the specified index

val getAt : string -> index:int -> char option

Get the character at ~index

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 : string -> string

Reverse a string

Note This function does not work with Unicode characters.

Examples

String.reverse "stressed" = "desserts"
val slice : ?to_:int -> string -> from:int -> string

Extract a substring from the specified indicies.

See Array.slice.

Query

val isEmpty : string -> bool

Check if a string is empty

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 : 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 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 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 : 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

Modify

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 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 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**"
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 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 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 : 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.capitalize "den" = "Den"
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 : string -> string

Like trim but only drops characters from the beginning of the string.

val trimRight : string -> string

Like trim but only drops characters from the end of the string.

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 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"

Deconstruct

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 : 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 : string -> f:(char -> unit) -> unit

Run f on each character in a string.

val fold : string -> initial:'a -> f:('a -> char -> 'a) -> 'a

Like Array.fold but the elements are Chars

Convert

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 toList : string -> char list

Returns a List of the individual characters in the given string.

Examples

String.toList "" = []
String.toList "abc" = ['a'; 'b'; 'c']

Compare

val equal : string -> string -> bool

Test two string for equality

val compare : string -> string -> int

Test two string for equality

type identity

The unique identity for Comparator

val comparator : (t, identity) Standard__.Core.Comparator.t