Skip to main content

String

Utility functions for String.

Live Editor
Result
Loading...

append

Alias for concat.

function append(appendValue: string): (str: string) => string
function append(str: string, appendValue: string): string

concat

Returns a new string with appendValue added after str.

function concat(appendValue: string): (str: string) => string
function concat(str: string, appendValue: string): string
S.concat('hello', 'world') // → 'helloworld'
pipe('ts', S.concat('belt')) // → 'tsbelt'

endsWith

Returns true if the given string ends with substr.

function endsWith(substr: string): (str: string) => boolean
function endsWith(str: string, substr: string): boolean
S.endsWith('hello', 'o') // → true
pipe('ts-belt', S.endsWith('o')) // → false

get

Returns Some(value), where value is a string consisting of the character at location n in the string, or None if the n is out of range.

function get(str: string, n: number): Option<string>
function get(n: number): (str: string) => Option<string>
S.get('hello', 1) // → Some('e')
pipe('ts-belt', S.get(9)) // → None

getUnsafe

Returns value, where value is a string consisting of the character at location n in the string, or undefined if the n is out of range.

function getUnsafe(str: string, n: number): string
function getUnsafe(n: number): (str: string) => string
S.getUnsafe('hello', 1) // → 'e'
pipe('world', S.getUnsafe(1)) // → 'o'

Returns Some(value) where value is the first character of the string, or None if the given string is empty.

function head(str: string): Option<string>
S.head('random-text') // → Some('r')

includes

Returns true if searchValue appears anywhere in the given string.

function includes(searchValue: string): (str: string) => boolean
function includes(str: string, searchValue: string): boolean
S.includes('hello', 'll') // → true
pipe('world', S.includes('ll')) // → false

indexOf

Returns Some(index), where index is the starting position of the first occurrence of searchValue within str.

function indexOf(searchValue: string): (str: string) => Option<number>
function indexOf(str: string, searchValue: string): Option<number>
S.indexOf('hello', 'e') // → Some(1)
pipe(['hello', 'world'], A.keepMap(S.indexOf('o'))) // → [4, 1]

isEmpty

Returns true if the provided string is empty.

function isEmpty(str: string): boolean
S.isEmpty('hello world') // → false
S.isEmpty('') // → true
pipe('ts-belt', S.isEmpty) // → false

isNotEmpty

Returns true if the provided string is not empty.

function isNotEmpty(str: string): boolean
S.isNotEmpty('hello world') // → true
S.isNotEmpty('') // → false
pipe('ts-belt', S.isNotEmpty) // → true

last

Returns Some(value) where value is the last character of the string, or None if the given string is empty.

function last(str: string): Option<string>
S.last('random-text') // → Some('t')

lastIndexOf

Returns Some(index), where index is the starting position of the last occurrence of searchValue within str.

function lastIndexOf(searchValue: string): (str: string) => Option<number>
function lastIndexOf(str: string, searchValue: string): Option<number>
S.lastIndexOf('ts,rescript', 's') // → Some(5)
S.lastIndexOf('ts,rescript', 'x') // → None
pipe(['hello', 'ts'], A.keepMap(S.lastIndexOf('l'))) // → [3]

length

Returns the length of the given string.

function length(str: string): number
S.length('hello') // → 5
pipe('ts-belt', S.length) // → 7

make

Converts the given value to a string.

function make<A>(value: A): string
S.make(['hello', 'world']) // → 'hello,world'
S.make(true) // → 'true'
pipe({}, S.make) // → '[object Object]'
pipe(100, S.make) // → '100'

match

Matches the given string against the provided regular expression, ir returns None if there is no match.

function match(str: string, regex: RegExp): Option<Array<string>>
function match(regex: RegExp): (str: string) => Option<Array<string>>

prepend

Returns a new string with prependValue added before str.

function prepend(prependValue: string): (str: string) => string
function prepend(str: string, prependValue: string): string
S.prepend('hello', 'world') // → 'worldhello'
pipe('ts', S.prepend('belt')) // → 'beltts'

remove

Returns a new string with the first occurrence of value removed from str.

function remove(value: string): (str: string) => string
function remove(str: string, value: string): string
S.remove('hello', 'l') // → 'helo'
pipe('ts-belt', S.remove('ts-')) // → 'belt'

removeAll

Returns a new string with every occurrence of value removed from str.

function removeAll(value: string): (str: string) => string
function removeAll(str: string, value: string): string
S.removeAll('hello', 'l') // → 'heo'
pipe('hXellXo wXXorXXld', S.removeAll('X')) // → 'hello world'

repeat

Returns a string consisting of n repetitions of str.

function repeat(str: string, n: number): string
function repeat(n: number): (str: string) => string

replace

Replaces the first occurrence of oldValue with newValue in the given string and returns a new string.

function replace(oldValue: string, newValue: string): (str: string) => string
function replace(str: string, oldValue: string, newValue: string): string

replaceAll

Replaces each occurrence of oldValue with newValue in the given string and returns a new string.

function replaceAll(oldValue: string, newValue: string): (str: string) => string
function replaceAll(str: string, oldValue: string, newValue: string): string

replaceByRe

Replaces the matched regular expression with newValue in the given string and returns a new string.

function replaceByRe(str: string, regex: RegExp, value: string): string
function replaceByRe(regex: RegExp, value: string): (str: string) => string

Returns Some(index), where index is the starting position of the first match of regular expression in the given string.

function search(str: string, regex: RegExp): Option<number>
function search(regex: RegExp): (str: string) => Option<number>

slice

Returns the substring of str starting at character start up to but not including end.

function slice(str: string, start: number, end: number): string
function slice(start: number, end: number): (str: string) => string

sliceToEnd

Returns the substring of str starting at character start to the end of the string.

function sliceToEnd(str: string, start: number): string
function sliceToEnd(start: number): (str: string) => string

split

Splits the given string at every occurrence of delimiter and returns an array of the resulting substrings.

function split(delimiter: string): (str: string) => Array<string>
function split(str: string, delimiter: string): Array<string>

splitAt

Splits the string at the given index, returning a tuple of the parts.

function splitAt(str: string, index: number): readonly [string, string]
function splitAt(index: number): (str: string) => readonly [string, string]

splitByRe

Splits the given string at every occurrence of regex and returns an array of the resulting substrings.

function splitByRe(str: string, regex: RegExp): Array<Option<string>>
function splitByRe(regex: RegExp): (str: string) => Array<Option<string>>

startsWith

Returns true if the given string starts with substr.

function startsWith(substr: string): (str: string) => boolean
function startsWith(str: string, substr: string): boolean
S.startsWith('hello', 'o') // → false
pipe('ts-belt', S.startsWith('ts')) // → true

toArray

Creates an array with one character of str per element.

function toArray(str: string): Array<string>

toLowerCase

Converts str to lower case.

function toLowerCase(str: string): string

toUpperCase

Converts str to upper case.

function toUpperCase(str: string): string

trim

Returns a new string with leading and trailing whitespace removed from str.

function trim(str: string): string
S.trim('  text') // → 'text'

trimEnd

Returns a new string with trailing whitespace removed from str.

function trimEnd(str: string): string
S.trimEnd('  text  ') // → '  text'

trimStart

Returns a new string with leading whitespace removed from str.

function trimStart(str: string): string
S.trimStart('  text  ') // → 'text  '