String

GitHub   Edit on GitHub

Utilities for working with strings.

Added in 0.2.0
versionchanges
0.1.0Originally named `strings`
0.2.0Renamed to `string`
1
import String from "string"

Types

Type declarations included in the String module.

String.Encoding

1
2
3
4
5
6
7
enum Encoding {
UTF8,
UTF16_BE,
UTF16_LE,
UTF32_BE,
UTF32_LE,
}

Byte encodings


Values

Functions for working with the String data type.

String.concat

Added in 0.2.0 No other changes yet.
1
concat : (String, String) -> String

Concatenate two strings.

Parameters:

param type description
str1 String The beginning string
str2 String The ending string

Returns:

type description
String The combined string

Examples:

1
String.concat("Foo", "Bar") == "FooBar"

String.length

Added in 0.1.0 No other changes yet.
1
length : String -> Number

Returns the character length of the input string.

Parameters:

param type description
string String The string to inspect

Returns:

type description
Number The number of characters in the string

Examples:

1
String.length("Hello world") == 11

String.byteLength

Added in 0.1.0 No other changes yet.
1
byteLength : String -> Number

Returns the byte length of the input string.

Parameters:

param type description
string String The string to inspect

Returns:

type description
Number The number of bytes in the string

Examples:

1
String.byteLength("🌾") == 4

String.indexOf

Added in 0.3.0 No other changes yet.
1
indexOf : (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

Returns:

type description
Option<Number> Some(position) containing the starting position of the substring if found or None otherwise

Examples:

1
String.indexOf("world", "Hello world") == Some(6)

String.lastIndexOf

Added in 0.5.3 No other changes yet.
1
lastIndexOf : (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

Returns:

type description
Option<Number> Some(position) containing the starting position of the substring if found or None otherwise

Examples:

1
String.lastIndexOf("world", "Hello world world") == Some(12)

String.charCodeAt

Added in 0.5.3 No other changes yet.
1
charCodeAt : (Number, 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

Returns:

type description
Number The character code at the provided position

Examples:

1
String.charCodeAt(5, "Hello world") == 32

String.charAt

Added in 0.4.0 No other changes yet.
1
charAt : (Number, 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

Returns:

type description
Char The character at the provided position

Examples:

1
String.charAt(5, "Hello world") == ' '

String.explode

Added in 0.3.0 No other changes yet.
1
explode : String -> Array<Char>

Split a string into its Unicode characters.

Parameters:

param type description
string String The string to split

Returns:

type description
Array<Char> An array containing all characters in the string

Examples:

1
String.explode("Hello") == [> 'H', 'e', 'l', 'l', 'o']

String.implode

Added in 0.3.0 No other changes yet.
1
implode : Array<Char> -> String

Create a string from an array of characters.

Parameters:

param type description
arr Array<Char> The array to combine

Returns:

type description
String A string representation of the array of characters

Examples:

1
String.implode([> 'H', 'e', 'l', 'l', 'o']) == "Hello"

String.reverse

Added in 0.4.5 No other changes yet.
1
reverse : String -> String

Create a string that is the given string reversed.

Parameters:

param type description
string String The string to reverse

Returns:

type description
String A string whose characters are in the reverse order of the given string

Examples:

1
String.reverse("olleH") == "Hello"

String.split

1
split : (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

Returns:

type description
Array<String> An array of substrings from the initial string

Examples:

1
String.split(" ", "Hello world") == [> "Hello", "world"]

String.slice

Added in 0.1.0 No other changes yet.
1
slice : (Number, Number, String) -> String

Get a portion of a string.

Parameters:

param type description
start Number The start position of the substring
to Number The end position of the substring, exclusive
string String The input string

Returns:

type description
String The substring from the initial string

Examples:

1
String.slice(0, 5, "Hello world") == "Hello"

String.contains

Added in 0.1.0 No other changes yet.
1
contains : (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

Returns:

type description
Bool true if the input string contains the search value or false otherwise

Examples:

1
String.contains("world", "Hello world") == true

String.startsWith

Added in 0.1.0 No other changes yet.
1
startsWith : (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

Returns:

type description
Bool true if the input string starts with the search value or false otherwise

Examples:

1
String.startsWith("Hello", "Hello world") == true

String.endsWith

Added in 0.1.0 No other changes yet.
1
endsWith : (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

Returns:

type description
Bool true if the input string ends with the search value or false otherwise

Examples:

1
String.endsWith("world", "Hello world") == true

String.replaceFirst

Added in 0.5.4 No other changes yet.
1
replaceFirst : (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

Returns:

type description
String A new string with the first occurrence of the search pattern replaced

Examples:

1
String.replaceFirst("🌾", "🌎", "Hello 🌾🌾") == "Hello 🌎🌾"

String.replaceLast

Added in 0.5.4 No other changes yet.
1
replaceLast : (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

Returns:

type description
String A new string with the last occurrence of the search pattern replaced

Examples:

1
String.replaceLast("🌾", "🌎", "Hello 🌾🌾") == "Hello 🌾🌎"

String.replaceAll

Added in 0.5.4 No other changes yet.
1
replaceAll : (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

Returns:

type description
String A new string with each occurrence of the search pattern replaced

Examples:

1
String.replaceAll("🌾", "🌎", "Hello 🌾🌾") == "Hello 🌎🌎"

String.encodeAt

Added in 0.4.0 No other changes yet.
1
encodeAt : (String, Encoding, Bytes, Number) -> Bytes

Encodes the given string into a byte sequence at the supplied position, excluding any byte-order marker, 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

Returns:

type description
Bytes A copy of the input bytes with the encoded string replaced at the given position

String.encodeAtWithBom

Added in 0.4.0 No other changes yet.
1
encodeAtWithBom : (String, Encoding, Bytes, Number) -> Bytes

Encodes the given string into a byte sequence at the supplied position, including any byte-order marker, 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

Returns:

type description
Bytes A copy of the input bytes with the encoded string replaced at the given position

String.encode

Added in 0.4.0 No other changes yet.
1
encode : (String, Encoding) -> Bytes

Encodes the given string using the given encoding scheme, excluding any byte-order marker.

Parameters:

param type description
string String The input string
encoding Encoding The encoding to use

Returns:

type description
Bytes The byte representation of the string in the given encoding

String.encodeWithBom

Added in 0.4.0 No other changes yet.
1
encodeWithBom : (String, Encoding) -> Bytes

Encodes the given string using the given encoding scheme, including any byte-order marker.

Parameters:

param type description
string String The input string
encoding Encoding The encoding to use

Returns:

type description
Bytes The byte representation of the string in the given encoding

String.decodeRange

Added in 0.4.0 No other changes yet.
1
decodeRange : (Bytes, Encoding, Number, Number) -> String

Decodes the given byte sequence of the specified range into a string, excluding any byte-order marker, using 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

Returns:

type description
String The decoded string

String.decodeRangeKeepBom

Added in 0.4.0 No other changes yet.
1
decodeRangeKeepBom : (Bytes, Encoding, Number, Number) -> String

Decodes the given byte sequence of the specified range into a string, including any byte-order marker, using 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

Returns:

type description
String The decoded string

String.decode

Added in 0.4.0 No other changes yet.
1
decode : (Bytes, Encoding) -> String

Decodes the given byte sequence into a string using the given encoding scheme, excluding any byte-order marker.

Parameters:

param type description
bytes Bytes The input bytes
encoding Encoding The encoding to use

Returns:

type description
String The decoded string

String.decodeKeepBom

Added in 0.4.0 No other changes yet.
1
decodeKeepBom : (Bytes, Encoding) -> String

Decodes the given byte sequence into a string using the given encoding scheme, including any byte-order marker.

Parameters:

param type description
bytes Bytes The input bytes
encoding Encoding The encoding to use

Returns:

type description
String The decoded string

String.forEachCodePoint

Added in 0.4.0 No other changes yet.
1
forEachCodePoint : ((Number -> Void), 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

Examples:

1
String.forEachCodePoint(print, "Hello world")

String.forEachCodePointi

Added in 0.4.0 No other changes yet.
1
forEachCodePointi : (((Number, Number) -> Void), 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

Examples:

1
String.forEachCodePointi((codepoint, index) => print((codepoint, index)), "Hello world")

String.trimStart

Added in 0.4.2 No other changes yet.
1
trimStart : String -> String

Trims the beginning of a string—removing any leading whitespace characters.

Parameters:

param type description
string String The string to be trimmed

Returns:

type description
String The trimmed string

Examples:

1
String.trimStart("   Hello World") == "Hello World"

String.trimEnd

Added in 0.4.2 No other changes yet.
1
trimEnd : String -> String

Trims the end of a string—removing any trailing whitespace characters.

Parameters:

param type description
string String The string to be trimmed

Returns:

type description
String The trimmed string

Examples:

1
String.trimEnd("Hello World   ") == "Hello World"

String.trim

Added in 0.4.2 No other changes yet.
1
trim : String -> String

Trims a string—removing all leading and trailing whitespace characters.

Parameters:

param type description
string String The string to be trimmed

Returns:

type description
String The trimmed string

Examples:

1
String.trim("   Hello World   ") == "Hello World"
This is a notification!