String
The string type and its methods.
Always available
No import needed. These names are in scope in every file.
String class
ts
class StringFinal: it cannot be subclassed.
Instance surface for the primitive string type. Strings are immutable: every method that "modifies" the string returns a new value; the receiver is never mutated.
Pattern-accepting methods (match, matchAll, replace, replaceAll, search, split) accept either a string (treated as a literal substring) or a RegExp.
String.length
ts
readonly length: intLength in bytes (not Unicode code points).
String.toLowerCase
ts
toLowerCase(): stringLowercase every ASCII letter. Non-ASCII passes through unchanged.
String.toUpperCase
ts
toUpperCase(): stringUppercase every ASCII letter. Non-ASCII passes through unchanged.
String.toTitleCase
ts
toTitleCase(): stringCapitalize the first character of every word. Words are separated by space, hyphen, or underscore.
String.toCamelCase
ts
toCamelCase(): stringConvert space-, hyphen-, or underscore-separated words to camelCase. The first word stays lowercase; subsequent words have their first letter uppercased.
String.toSnakeCase
ts
toSnakeCase(): stringConvert to snake_case. Spaces and hyphens become underscores; uppercase ASCII letters introduce an underscore before being lowercased (except in runs of consecutive uppercase letters).
String.toKebabCase
ts
toKebabCase(): stringConvert to kebab-case. Spaces and underscores become hyphens; uppercase ASCII letters introduce a hyphen before being lowercased (except in runs of consecutive uppercase letters).
String.endsWith
ts
endsWith(searchString: string): booleanTrue when the string ends with searchString.
String.includes
ts
includes(searchString: string): booleanTrue when searchString appears anywhere in the string.
String.indexOf
ts
indexOf(searchString: string, fromIndex?: int): intReturn the index of the first occurrence of searchString at or after fromIndex (default 0), or -1 if not found.
String.lastIndexOf
ts
lastIndexOf(searchString: string): intReturn the index of the last occurrence of searchString, or -1 if not found.
String.match
ts
match(regexp: any): anyMatch against a string or RegExp. Returns null on no match, or a match object (full match at key 0, captured groups at 1..n, plus index, input, length). With a global RegExp, returns an array of full-match strings instead.
String.matchAll
ts
matchAll(regexp: any): Array<any>Match all occurrences of a string or RegExp. Returns an array of match objects (same shape as match). Always returns an array, never null.
String.search
ts
search(pattern: any): intReturn the index of the first match for a string or RegExp, or -1 if not found. Like indexOf but accepts patterns.
String.startsWith
ts
startsWith(searchString: string): booleanTrue when the string starts with searchString.
String.at
ts
at(index: int): stringReturn the single character at index. Negative indices count from the end. Returns the empty string if out of range.
String.charAt
ts
charAt(index: int): stringReturn the single character at index. Returns the empty string if out of range.
String.charCodeAt
ts
charCodeAt(index: int): intReturn the byte value at index, or NaN if out of range.
String.slice
ts
slice(start?: int, end?: int): stringReturn the substring from start (default 0) up to but not including end (default end-of-string). Negative indices count from the end.
String.substring
ts
substring(start: int, end?: int): stringReturn the substring from start up to but not including end (default end-of-string). Negative indices clamp to 0; if start > end, the arguments are swapped.
String.left
ts
left(n: int): stringReturn the first n characters. If n is at least the length of the string, the whole string. n of 0 gives "". A negative n throws a RangeError.
String.right
ts
right(n: int): stringReturn the last n characters. If n is at least the length of the string, the whole string. n of 0 gives "". A negative n throws a RangeError.
String.mid
ts
mid(start: int, n?: int): stringReturn n characters starting at the 0-based index start, or everything to the end when n is omitted. A start past the end gives "". A negative start or n throws a RangeError.
String.concat
ts
concat(...strings: Array<string>): stringReturn the receiver concatenated with each argument in order.
String.padEnd
ts
padEnd(targetLength: int, padString?: string): stringPad the right end with padString (default " ") until the total length reaches targetLength. Returns the receiver unchanged if it is already long enough.
String.padStart
ts
padStart(targetLength: int, padString?: string): stringPad the left end with padString (default " ") until the total length reaches targetLength. Returns the receiver unchanged if it is already long enough.
String.repeat
ts
repeat(count: int): stringRepeat the string count times. count must be non-negative.
String.replace
ts
replace(search: any, replacement: any): stringReplace the first match of search (a string or RegExp) with replacement. replacement may be a string or a function called with the match details.
String.replaceAll
ts
replaceAll(search: any, replacement: any): stringReplace every match of search (a string or RegExp) with replacement. replacement may be a string or a function called with each match's details.
String.reverse
ts
reverse(): stringReturn the string with its bytes reversed.
String.split
ts
split(separator: any, limit?: int): Array<string>Split the string on separator (a string or RegExp). Returns an array of pieces, capped at limit when provided.
String.toString
ts
toString(): stringReturn the receiver. Provided for symmetry with other types.
String.trim
ts
trim(): stringStrip leading and trailing whitespace.
String.trimEnd
ts
trimEnd(): stringStrip trailing whitespace.
String.trimStart
ts
trimStart(): stringStrip leading whitespace.
String.truncate
ts
truncate(maxLen: int, suffix?: string): stringTruncate to at most maxLen bytes, appending suffix (default "...") when truncation actually occurs. The total length of the result, including suffix, will not exceed maxLen.
String.valueOf
ts
valueOf(): stringReturn the receiver (the string itself), like toString().
String.fromBase64
ts
static fromBase64(encoded: string): stringDecode a base64 string into its raw byte string.
String.fromHex
ts
static fromHex(hex: string): stringDecode a hex string into its raw byte string.
String.fromCharCode
ts
static fromCharCode(...codes: Array<int>): stringBuild a string from UTF-16 code units, like TypeScript. Each argument is reduced to 16 bits. Strings are UTF-8, so units are stored as UTF-8: a high plus low surrogate pair becomes one code point, and a lone surrogate becomes U+FFFD.
Example
ts
console.log(String.fromCharCode(72, 105)); // HiString.toBase64
ts
static toBase64(data: string): stringEncode a byte string as base64.
String.toHex
ts
static toHex(data: string): stringEncode a byte string as lowercase hex.
Generated from lang/types/builtin_string.d.tks in the ThicketScript repository.