String
Utilities for working with strings.
Edit on GitHubAdded in 0.2.0
| version | changes |
|---|---|
0.1.0 | Originally named `strings` |
0.2.0 | Renamed to `string` |
from "string" include String
Type declarations included in the String module.
enum Encoding {
UTF8,
UTF16_BE,
UTF16_LE,
UTF32_BE,
UTF32_LE,
}
Byte encodings
Functions and constants included in the String module.
Added in 0.2.0
No other changes yet.
concat : (str1: String, str2: String) => String
Concatenate two strings.
Parameters:
| param | type | description |
|---|---|---|
str1 | String | The beginning string |
str2 | String | The ending string |
str1: The beginning stringstr2: The ending stringReturns:
| type | description |
|---|---|
String | The combined string |
String: The combined stringExamples:
String.concat("Foo", "Bar") == "FooBar"
Added in 0.1.0
No other changes yet.
length : (string: String) => Number
Returns the character length of the input string.
Parameters:
| param | type | description |
|---|---|---|
string | String | The string to inspect |
string: The string to inspectReturns:
| type | description |
|---|---|
Number | The number of characters in the string |
Number: The number of characters in the stringExamples:
String.length("Hello world") == 11
Added in 0.1.0
No other changes yet.
byteLength : (string: String) => Number
Returns the byte length of the input string.
Parameters:
| param | type | description |
|---|---|---|
string | String | The string to inspect |
string: The string to inspectReturns:
| type | description |
|---|---|
Number | The number of bytes in the string |
Number: The number of bytes in the stringExamples:
String.byteLength("🌾") == 4
Added in 0.6.0
No other changes yet.
isEmpty : (string: String) => Bool
Determines if the string contains no characters.
Parameters:
| param | type | description |
|---|---|---|
string | String | The string to inspect |
string: The string to inspectReturns:
| type | description |
|---|---|
Bool | true if the string is empty and false otherwise |
Bool: true if the string is empty and false otherwiseAdded in 0.3.0
No other changes yet.
indexOf : (search: String, string: String) => Option<Number>
Finds the first position of a substring in the input string.
Parameters:
| param | type | description |
|---|---|---|
search | String | The substring to find |
string | String | The string to inspect |
search: The substring to findstring: The string to inspectReturns:
| type | description |
|---|---|
Option<Number> | Some(position) containing the starting position of the substring if found or None otherwise |
Option<Number>: Some(position) containing the starting position of the substring if found or None otherwiseExamples:
String.indexOf("world", "Hello world") == Some(6)
Added in 0.5.3
No other changes yet.
lastIndexOf : (search: String, string: String) => Option<Number>
Finds the last position of a substring in the input string.
Parameters:
| param | type | description |
|---|---|---|
search | String | The substring to find |
string | String | The string to inspect |
search: The substring to findstring: The string to inspectReturns:
| type | description |
|---|---|
Option<Number> | Some(position) containing the starting position of the substring if found or None otherwise |
Option<Number>: Some(position) containing the starting position of the substring if found or None otherwiseExamples:
String.lastIndexOf("world", "Hello world world") == Some(12)
Added in 0.5.3
No other changes yet.
charCodeAt : (position: Number, string: String) => Number
Get the Unicode code point at the position in the input string.
Parameters:
| param | type | description |
|---|---|---|
position | Number | The position to check |
string | String | The string to search |
position: The position to checkstring: The string to searchReturns:
| type | description |
|---|---|
Number | The character code at the provided position |
Number: The character code at the provided positionThrows:
Failure(String)
- When the
positionis out of bounds
MalformedUnicode
- When the
stringis malformed
Examples:
String.charCodeAt(5, "Hello world") == 32
Added in 0.4.0
No other changes yet.
charAt : (position: Number, string: String) => Char
Get the character at the position in the input string.
Parameters:
| param | type | description |
|---|---|---|
position | Number | The position to check |
string | String | The string to search |
position: The position to checkstring: The string to searchReturns:
| type | description |
|---|---|
Char | The character at the provided position |
Char: The character at the provided positionThrows:
Failure(String)
- When the
positionis out of bounds
MalformedUnicode
- When the
stringis malformed
Examples:
String.charAt(5, "Hello world") == ' '
Added in 0.3.0
No other changes yet.
explode : (string: String) => Array<Char>
Split a string into its Unicode characters.
Parameters:
| param | type | description |
|---|---|---|
string | String | The string to split |
string: The string to splitReturns:
| type | description |
|---|---|
Array<Char> | An array containing all characters in the string |
Array<Char>: An array containing all characters in the stringThrows:
MalformedUnicode
- When the
stringis malformed
Examples:
String.explode("Hello") == [> 'H', 'e', 'l', 'l', 'o']
Added in 0.3.0
No other changes yet.
implode : (arr: Array<Char>) => String
Create a string from an array of characters.
Parameters:
| param | type | description |
|---|---|---|
arr | Array<Char> | The array to combine |
arr: The array to combineReturns:
| type | description |
|---|---|
String | A string representation of the array of characters |
String: A string representation of the array of charactersExamples:
String.implode([> 'H', 'e', 'l', 'l', 'o']) == "Hello"
Added in 0.4.5
No other changes yet.
reverse : (string: String) => String
Create a string that is the given string reversed.
Parameters:
| param | type | description |
|---|---|---|
string | String | The string to reverse |
string: The string to reverseReturns:
| type | description |
|---|---|
String | A string whose characters are in the reverse order of the given string |
String: A string whose characters are in the reverse order of the given stringExamples:
String.reverse("olleH") == "Hello"
split : (separator: String, string: String) => Array<String>
Split a string by the given separator.
Parameters:
| param | type | description |
|---|---|---|
separator | String | The separator to split on |
string | String | The string to split |
separator: The separator to split onstring: The string to splitReturns:
| type | description |
|---|---|
Array<String> | An array of substrings from the initial string |
Array<String>: An array of substrings from the initial stringThrows:
MalformedUnicode
- When the
stringis malformed
Examples:
String.split(" ", "Hello world") == [> "Hello", "world"]
Added in 0.1.0
| version | changes |
|---|---|
0.6.0 | Default `end` to the String length |
slice : (start: Number, ?end: Number, string: String) => String
Get a portion of a string.
Parameters:
| param | type | description |
|---|---|---|
start | Number | The start position of the substring |
?end | Number | The end position of the substring, exclusive |
string | String | The input string |
start: The start position of the substring?end: The end position of the substring, exclusivestring: The input stringReturns:
| type | description |
|---|---|
String | The substring from the initial string |
String: The substring from the initial stringThrows:
IndexOutOfBounds
- When
startis out of bounds - When
endis out of bounds
InvalidArgument(String)
- When the
startindex is not an integer - When the
toindex is not an integer - When
startis greater thanend
Examples:
String.slice(0, end=5, "Hello world") == "Hello"
String.slice(0, "Hello world") == "Hello world"
Added in 0.1.0
No other changes yet.
contains : (search: String, string: String) => Bool
Check if a string contains a substring.
Parameters:
| param | type | description |
|---|---|---|
search | String | The substring to check |
string | String | The string to search |
search: The substring to checkstring: The string to searchReturns:
| type | description |
|---|---|
Bool | true if the input string contains the search value or false otherwise |
Bool: true if the input string contains the search value or false otherwiseExamples:
String.contains("world", "Hello world") == true
Added in 0.1.0
No other changes yet.
startsWith : (search: String, string: String) => Bool
Check if a string begins with another string.
Parameters:
| param | type | description |
|---|---|---|
search | String | The string to compare to the start |
string | String | The string to search |
search: The string to compare to the startstring: The string to searchReturns:
| type | description |
|---|---|
Bool | true if the input string starts with the search value or false otherwise |
Bool: true if the input string starts with the search value or false otherwiseExamples:
String.startsWith("Hello", "Hello world") == true
Added in 0.1.0
No other changes yet.
endsWith : (search: String, string: String) => Bool
Check if a string ends with another string.
Parameters:
| param | type | description |
|---|---|---|
search | String | The string to compare to the end |
string | String | The string to search |
search: The string to compare to the endstring: The string to searchReturns:
| type | description |
|---|---|
Bool | true if the input string ends with the search value or false otherwise |
Bool: true if the input string ends with the search value or false otherwiseExamples:
String.endsWith("world", "Hello world") == true
Added in 0.5.4
No other changes yet.
replaceFirst :
(searchPattern: String, replacement: String, string: String) => String
Replaces the first appearance of the search pattern in the string with the replacement value.
Parameters:
| param | type | description |
|---|---|---|
searchPattern | String | The string to replace |
replacement | String | The replacement |
string | String | The string to change |
searchPattern: The string to replacereplacement: The replacementstring: The string to changeReturns:
| type | description |
|---|---|
String | A new string with the first occurrence of the search pattern replaced |
String: A new string with the first occurrence of the search pattern replacedExamples:
String.replaceFirst("🌾", "🌎", "Hello 🌾🌾") == "Hello 🌎🌾"
Added in 0.5.4
No other changes yet.
replaceLast :
(searchPattern: String, replacement: String, string: String) => String
Replaces the last appearance of the search pattern in the string with the replacement value.
Parameters:
| param | type | description |
|---|---|---|
searchPattern | String | The string to replace |
replacement | String | The replacement |
string | String | The string to change |
searchPattern: The string to replacereplacement: The replacementstring: The string to changeReturns:
| type | description |
|---|---|
String | A new string with the last occurrence of the search pattern replaced |
String: A new string with the last occurrence of the search pattern replacedExamples:
String.replaceLast("🌾", "🌎", "Hello 🌾🌾") == "Hello 🌾🌎"
Added in 0.5.4
No other changes yet.
replaceAll :
(searchPattern: String, replacement: String, string: String) => String
Replaces every appearance of the search pattern in the string with the replacement value.
Parameters:
| param | type | description |
|---|---|---|
searchPattern | String | The string to replace |
replacement | String | The replacement |
string | String | The string to change |
searchPattern: The string to replacereplacement: The replacementstring: The string to changeReturns:
| type | description |
|---|---|
String | A new string with each occurrence of the search pattern replaced |
String: A new string with each occurrence of the search pattern replacedExamples:
String.replaceAll("🌾", "🌎", "Hello 🌾🌾") == "Hello 🌎🌎"
Added in 0.4.0
| version | changes |
|---|---|
0.6.0 | Added `includeBom` default argument |
encodeAt :
(string: String, encoding: Encoding, dest: Bytes, destPos: Number,
?includeBom: Bool) => Bytes
Encodes the given string into a byte sequence at the supplied position using the encoding scheme provided.
Parameters:
| param | type | description |
|---|---|---|
string | String | The input string |
encoding | Encoding | The encoding to use |
dest | Bytes | The byte sequence that will be copied |
destPos | Number | The location in the byte sequence to write the output |
?includeBom | Bool | Whether or not to include a byte order marker (false by default) |
string: The input stringencoding: The encoding to usedest: The byte sequence that will be copieddestPos: The location in the byte sequence to write the output?includeBom: Whether or not to include a byte order marker (false by default)Returns:
| type | description |
|---|---|
Bytes | A copy of the input bytes with the encoded string replaced at the given position |
Bytes: A copy of the input bytes with the encoded string replaced at the given positionThrows:
InvalidArgument(String)
- When
destPosis not an integer - When
destPosis negative
Added in 0.4.0
| version | changes |
|---|---|
0.6.0 | Added `includeBom` default argument |
encode : (string: String, encoding: Encoding, ?includeBom: Bool) => Bytes
Encodes the given string using the given encoding scheme.
Parameters:
| param | type | description |
|---|---|---|
string | String | The input string |
encoding | Encoding | The encoding to use |
?includeBom | Bool | Whether or not to include a byte order marker (false by default) |
string: The input stringencoding: The encoding to use?includeBom: Whether or not to include a byte order marker (false by default)Returns:
| type | description |
|---|---|
Bytes | The byte representation of the string in the given encoding |
Bytes: The byte representation of the string in the given encodingAdded in 0.4.0
| version | changes |
|---|---|
0.6.0 | Added `keepBom` default argument |
decodeRange :
(bytes: Bytes, encoding: Encoding, start: Number, size: Number,
?keepBom: Bool) => String
Decodes the given byte sequence of the specified range into a string using the encoding scheme provided.
Parameters:
| param | type | description |
|---|---|---|
bytes | Bytes | The input bytes |
encoding | Encoding | The encoding to use |
start | Number | The byte offset to begin decoding from |
size | Number | The maximum number of bytes to decode |
?keepBom | Bool | Whether or not to include a byte order marker (false by default) |
bytes: The input bytesencoding: The encoding to usestart: The byte offset to begin decoding fromsize: The maximum number of bytes to decode?keepBom: Whether or not to include a byte order marker (false by default)Returns:
| type | description |
|---|---|
String | The decoded string |
String: The decoded stringThrows:
InvalidArgument(String)
- When
startis not an integer - When
startis negative - When
sizeis not an integer - When
sizeis negative
Added in 0.4.0
| version | changes |
|---|---|
0.6.0 | Added `keepBom` default argument |
decode : (bytes: Bytes, encoding: Encoding, ?keepBom: Bool) => String
Decodes the given byte sequence into a string using the given encoding scheme.
Parameters:
| param | type | description |
|---|---|---|
bytes | Bytes | The input bytes |
encoding | Encoding | The encoding to use |
?keepBom | Bool | Whether or not to include a byte order marker (false by default) |
bytes: The input bytesencoding: The encoding to use?keepBom: Whether or not to include a byte order marker (false by default)Returns:
| type | description |
|---|---|
String | The decoded string |
String: The decoded stringAdded in 0.4.0
No other changes yet.
forEachCodePoint : (fn: (Number => Void), str: String) => Void
Iterates over Unicode code points in a string.
Parameters:
| param | type | description |
|---|---|---|
fn | Number => Void | The iterator function |
str | String | The string to iterate |
fn: The iterator functionstr: The string to iterateExamples:
String.forEachCodePoint(print, "Hello world")
Added in 0.4.0
No other changes yet.
forEachCodePointi : (fn: ((Number, Number) => Void), str: String) => Void
Iterates over Unicode code points in a string. This is the same as
forEachCodePoint, but provides the code point’s index in the string
as the second argument to the iterator function.
Parameters:
| param | type | description |
|---|---|---|
fn | (Number, Number) => Void | The iterator function |
str | String | The string to iterate |
fn: The iterator functionstr: The string to iterateExamples:
String.forEachCodePointi((codepoint, index) => print((codepoint, index)), "Hello world")
Added in 0.6.5
No other changes yet.
forEachChar : (fn: (Char => Void), str: String) => Void
Iterates over Unicode characters in a string.
Parameters:
| param | type | description |
|---|---|---|
fn | Char => Void | The iterator function |
str | String | The string to iterate |
fn: The iterator functionstr: The string to iterateExamples:
String.forEachChar(print, "Hello world")
Added in 0.6.5
No other changes yet.
forEachChari : (fn: ((Char, Number) => Void), str: String) => Void
Iterates over Unicode characters in a string. This is the same as
forEachChar, but provides the characters’s index in the string
as the second argument to the iterator function.
Parameters:
| param | type | description |
|---|---|---|
fn | (Char, Number) => Void | The iterator function |
str | String | The string to iterate |
fn: The iterator functionstr: The string to iterateExamples:
String.forEachChari((char, index) => print((char, index)), "Hello world")
Added in 0.6.5
No other changes yet.
map : (fn: (Char => Char), str: String) => String
Builds a new string by mapping Unicode characters.
Parameters:
| param | type | description |
|---|---|---|
fn | Char => Char | The mapping function |
str | String | The string to map |
fn: The mapping functionstr: The string to mapExamples:
assert String.map((c) => 'a', "Hello world") == "aaaaaaaaaaa"
Added in 0.6.5
No other changes yet.
mapi : (fn: ((Char, Number) => Char), str: String) => String
Builds a new string by mapping Unicode characters. This is the same as
mapChar, but provides the characters’s index in the string
as the second argument to the mapping function.
Parameters:
| param | type | description |
|---|---|---|
fn | (Char, Number) => Char | The mapping function |
str | String | The string to map |
fn: The mapping functionstr: The string to mapExamples:
assert String.mapi((char, index) => String.charAt(0, toString(index)), "Hello world") == "01234567891"
Added in 0.4.2
No other changes yet.
trimStart : (string: String) => String
Trims the beginning of a string—removing any leading whitespace characters.
Parameters:
| param | type | description |
|---|---|---|
string | String | The string to be trimmed |
string: The string to be trimmedReturns:
| type | description |
|---|---|
String | The trimmed string |
String: The trimmed stringExamples:
String.trimStart(" Hello World") == "Hello World"
Added in 0.4.2
No other changes yet.
trimEnd : (string: String) => String
Trims the end of a string—removing any trailing whitespace characters.
Parameters:
| param | type | description |
|---|---|---|
string | String | The string to be trimmed |
string: The string to be trimmedReturns:
| type | description |
|---|---|
String | The trimmed string |
String: The trimmed stringExamples:
String.trimEnd("Hello World ") == "Hello World"
Added in 0.4.2
No other changes yet.
trim : (string: String) => String
Trims a string—removing all leading and trailing whitespace characters.
Parameters:
| param | type | description |
|---|---|---|
string | String | The string to be trimmed |
string: The string to be trimmedReturns:
| type | description |
|---|---|
String | The trimmed string |
String: The trimmed stringExamples:
String.trim(" Hello World ") == "Hello World"
Added in 0.6.0
No other changes yet.
toAsciiLowercase : (string: String) => String
Converts all ASCII uppercase characters in the string to lowercase.
Parameters:
| param | type | description |
|---|---|---|
string | String | The string to convert |
string: The string to convertReturns:
| type | description |
|---|---|
String | The lowercased string |
String: The lowercased stringExamples:
assert String.toAsciiLowercase("aBc123") == "abc123"
Added in 0.6.0
No other changes yet.
toAsciiUppercase : (string: String) => String
Converts all ASCII lowercase characters in the string to uppercase.
Parameters:
| param | type | description |
|---|---|---|
string | String | The string to convert |
string: The string to convertReturns:
| type | description |
|---|---|
String | The uppercased string |
String: The uppercased stringExamples:
assert String.toAsciiUppercase("aBc123") == "ABC123"