Skip to content

String ​

The string type and its methods.

Always available

No import needed. These names are in scope in every file.

String class ​

ts
class String

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

Length in bytes (not Unicode code points).

String.toLowerCase ​

ts
toLowerCase(): string

Lowercase every ASCII letter. Non-ASCII passes through unchanged.

String.toUpperCase ​

ts
toUpperCase(): string

Uppercase every ASCII letter. Non-ASCII passes through unchanged.

String.toTitleCase ​

ts
toTitleCase(): string

Capitalize the first character of every word. Words are separated by space, hyphen, or underscore.

String.toCamelCase ​

ts
toCamelCase(): string

Convert space-, hyphen-, or underscore-separated words to camelCase. The first word stays lowercase; subsequent words have their first letter uppercased.

String.toSnakeCase ​

ts
toSnakeCase(): string

Convert 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(): string

Convert 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): boolean

True when the string ends with searchString.

String.includes ​

ts
includes(searchString: string): boolean

True when searchString appears anywhere in the string.

String.indexOf ​

ts
indexOf(searchString: string, fromIndex?: int): int

Return 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): int

Return the index of the last occurrence of searchString, or -1 if not found.

String.match ​

ts
match(regexp: any): any

Match 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.

ts
search(pattern: any): int

Return 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): boolean

True when the string starts with searchString.

String.at ​

ts
at(index: int): string

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

Return the single character at index. Returns the empty string if out of range.

String.charCodeAt ​

ts
charCodeAt(index: int): int

Return the byte value at index, or NaN if out of range.

String.slice ​

ts
slice(start?: int, end?: int): string

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

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

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

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

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

Return the receiver concatenated with each argument in order.

String.padEnd ​

ts
padEnd(targetLength: int, padString?: string): string

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

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

Repeat the string count times. count must be non-negative.

String.replace ​

ts
replace(search: any, replacement: any): string

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

Replace 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(): string

Return 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(): string

Return the receiver. Provided for symmetry with other types.

String.trim ​

ts
trim(): string

Strip leading and trailing whitespace.

String.trimEnd ​

ts
trimEnd(): string

Strip trailing whitespace.

String.trimStart ​

ts
trimStart(): string

Strip leading whitespace.

String.truncate ​

ts
truncate(maxLen: int, suffix?: string): string

Truncate 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(): string

Return the receiver (the string itself), like toString().

String.fromBase64 ​

ts
static fromBase64(encoded: string): string

Decode a base64 string into its raw byte string.

String.fromHex ​

ts
static fromHex(hex: string): string

Decode a hex string into its raw byte string.

String.fromCharCode ​

ts
static fromCharCode(...codes: Array<int>): string

Build 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)); // Hi

String.toBase64 ​

ts
static toBase64(data: string): string

Encode a byte string as base64.

String.toHex ​

ts
static toHex(data: string): string

Encode a byte string as lowercase hex.


Generated from lang/types/builtin_string.d.tks in the ThicketScript repository.