API reference

String methods

32/33Coverage22/33Strict (zero caveats)

The String surface is nearly complete. Comparison and substring search are binary-safe (length-headed, NUL-tolerant); the open edges are real Unicode — normalization, surrogate-pair code points, and locale collation — which this compiler has no tables for and does not fake.

Exact Works, with caveats Not supported

+ (concatenation)

Exact
string + string → string

Joins two strings. A null operand stringifies as "null", matching JavaScript — including a `number | null` held in a parameter, a local, an object field, or returned from a function.

console.log('x' + null)   // xnull   (matches JS)

.length

Exact
length: number

The number of bytes in the string. This compiler's strings are byte sequences, so for ASCII/Latin-1 text this equals the character count.

console.log('Hello'.length)   // 5

.slice(start?, end?)

Exact
slice(start?: number, end?: number): string

Returns a section of the string without mutating the original. Negative indices count from the end.

Parameters
startnumber
Zero-based start index; negative counts from the end.
end?number
Exclusive end index; defaults to the string length.
Returns

string — A new string spanning [start, end).

console.log('Hello, World!'.slice(7, 12))   // World
console.log('Hello, World!'.slice(-6))      // orld!
See it in a full example →

.substring(start, end?)

Exact
substring(start: number, end?: number): string

Like slice, but negative arguments clamp to 0 and a start greater than end is swapped, matching JavaScript.

Parameters
startnumber
Start index; clamped to 0 if negative.
end?number
Exclusive end index; defaults to the string length.
Returns

string — A new string between the two indices.

console.log('Hello, World!'.substring(0, 5))   // Hello
console.log('Hello, World!'.substring(5, 0))   // Hello   (swapped)
See it in a full example →

.substr(start, length?)

Exact
substr(start: number, length?: number): string

Deprecated but common: extracts length characters from start. A negative start counts from the end; length defaults to the rest of the string.

Parameters
startnumber
Start index; negative counts from the end.
length?number
Number of characters to take; defaults to the rest of the string.
Returns

string — A new string of at most length characters from start.

console.log('Hello, World!'.substr(7, 5))   // World
console.log('Hello, World!'.substr(-6))     // orld!
See it in a full example →

.indexOf(substr, fromIndex?)

Exact
indexOf(substr: string, fromIndex?: number): number

Returns the byte index of the first occurrence of the substring at or after the optional fromIndex, or -1 if absent. Binary-safe past an embedded NUL.

Returns

number — First match index, or -1.

console.log('the quick brown fox'.indexOf('brown'))   // 10

.lastIndexOf(substr, fromIndex?)

Works, with caveats
lastIndexOf(substr: string, fromIndex?: number): number

Returns the byte index of the LAST occurrence of the substring at or before the optional fromIndex, or -1. Binary-safe (a descending memcmp scan); an empty needle returns the string length.

Returns

number — Last match index, or -1.

console.log('path/to/file'.lastIndexOf('/'))   // 7
⚠ Differences from standard
  • Returns a byte offset, not a UTF-16 index — like .indexOf on non-ASCII text.

.includes(substr, position?)

Exact
includes(substr: string, position?: number): boolean

Whether the string contains the substring, searching from the optional position. Binary-safe (searches with memmem over a length header).

Returns

boolean — true if the substring is present.

console.log('report.pdf'.includes('.pdf'))   // true

.startsWith(prefix, position?)

Exact
startsWith(prefix: string, position?: number): boolean

Whether the string begins with the prefix at the optional position (default 0). Returns false when the string is shorter than the prefix.

Returns

boolean — true if the string starts with the prefix.

console.log('https://example.com'.startsWith('https'))   // true
See it in a full example →

.endsWith(suffix, endPosition?)

Exact
endsWith(suffix: string, endPosition?: number): boolean

Whether the string ends with the suffix, treating the string as if it ended at the optional endPosition. Returns false when the string is shorter than the suffix.

Returns

boolean — true if the string ends with the suffix.

console.log('report.pdf'.endsWith('.pdf'))   // true
See it in a full example →

.replace(from, to)

Exact
replace(from: string, to: string): string

Replaces the first occurrence of `from` with `to`, returning the original string when there is no match. See RegExp for regex-pattern edges.

Returns

string — A new string with the first match replaced.

console.log('Hello World'.replace('World', 'TypeGo'))   // Hello TypeGo
See it in a full example →

.split(sep, limit?)

Exact
split(sep: string | RegExp, limit?: number): string[]

Splits the string on a string or RegExp separator into an array. An empty separator splits into individual characters; the optional limit caps the result to the first limit segments — all matching JavaScript.

Returns

string[] — The array of substrings (at most limit of them).

console.log('one,two,three'.split(','))      // ['one','two','three']
console.log('a-b-c-d'.split('-', 2))        // ['a','b']

.trim()

Exact
trim(): string

Strips leading and trailing whitespace. Uses the full JS WhiteSpace/LineTerminator set (U+00A0, U+1680, U+2000–200A, U+2028/29, U+202F, U+205F, U+3000, U+FEFF — UTF-8-aware), not just ASCII.

Returns

string — The trimmed string.

console.log('  hello world  '.trim())   // hello world
See it in a full example →

.trimStart() / .trimEnd()

Exact
trimStart(): string; trimEnd(): string

Strips whitespace from one side only, using the same full whitespace set as trim().

Returns

string — The one-sided-trimmed string.

console.log('[' + '  hi  '.trimStart() + ']')   // [hi  ]
See it in a full example →

.toString()

Exact
toString(): string

Identity on a string, matching JavaScript. Kept because Node code habitually calls it on values that are Buffers there but strings here (spawnSync results, stream chunks).

Returns

string — The same string.

.toUpperCase()

Exact
toUpperCase(): string

Returns the string uppercased by the Unicode Default Case Conversion, as Node: every plane's simple mappings plus the SpecialCasing expansions ('ß' becomes 'SS', 'fi' becomes 'FI'); an embedded NUL is an ordinary character.

Returns

string — The uppercased string.

console.log('straße'.toUpperCase())   // STRASSE
See it in a full example →

.toLowerCase()

Exact
toLowerCase(): string

Returns the string lowercased by the Unicode Default Case Conversion, as Node — including the context-sensitive final sigma ('ΟΔΥΣΣΕΥΣ' becomes 'οδυσσευς') and the 'İ' expansion.

Returns

string — The lowercased string.

console.log('ΟΔΥΣΣΕΥΣ'.toLowerCase())   // οδυσσευς
See it in a full example →

.repeat(n)

Exact
repeat(n: number): string

Returns the string repeated n times.

Returns

string — The repeated string.

console.log('ab'.repeat(3))   // ababab

.padStart(len, pad?)

Exact
padStart(len: number, pad?: string): string

Pads the start of the string to a target length. An empty pad string is a no-op, matching JavaScript.

Parameters
lennumber
Target total length.
pad?string
Pad string; defaults to a space.
Returns

string — The padded string.

console.log('7'.padStart(3, '0'))   // 007

.padEnd(len, pad?)

Exact
padEnd(len: number, pad?: string): string

Pads the end of the string to a target length. Same empty-pad rule as padStart.

Parameters
lennumber
Target total length.
pad?string
Pad string; defaults to a space.
Returns

string — The padded string.

console.log('7'.padEnd(3, '.'))   // 7..

.concat(...values)

Exact
concat(...values: string[]): string

Returns the string followed by each argument, left to right. Arguments are converted with ToString, as in a template literal: an object's toString() is asked before its valueOf() (the + operator asks valueOf() first). A spread array contributes each of its elements.

Parameters
...valuesstring[]
Values to append; none returns the string itself.
Returns

string — The concatenated string.

console.log('a'.concat('b', 'c'))          // abc
console.log('s:'.concat(...['p', 'q']))   // s:pq
See it in a full example →

.charCodeAt(i)

Exact
charCodeAt(i: number): number

Returns the byte-space code unit at index i. Bounds-checked: an out-of-range index (negative or >= length) returns NaN, as real JS — the result is a double for exactly that reason.

Returns

number — The code unit, or NaN if out of range.

console.log('Hello'.charCodeAt(0))   // 72
See it in a full example →

.at(i)

Exact
at(i: number): string | undefined

Returns the character at index i, with negative indices counting from the end; an out-of-range index is a real undefined, as in Node.

Returns

string | undefined — The character at i, or undefined if i is out of range.

.charAt(i)

Exact
charAt(i: number): string

Returns the character at index i. Never wraps a negative index from the end — always "" for any out-of-range i, matching real JS's distinction from .at().

Returns

string — The character at i, or "" if out of range.

.codePointAt(i)

Works, with caveats
codePointAt(i: number): number | undefined

Returns the code point at index i, or undefined if out of range.

Returns

number | undefined — The code point, or undefined if the index is out of range (as in Node).

console.log('Hello'.codePointAt(0))   // 72
console.log('Hello'.codePointAt(99) ?? -1)   // -1
See it in a full example →
⚠ Differences from standard
  • Positions are byte offsets in this compiler's UTF-8 strings: at the first byte of a character the result is that character's code point, at a continuation byte that byte; Node indexes UTF-16 code units.
  • An out-of-range index returns a real undefined (as in Node), so the type is number | undefined — narrow, ?? n, or ! before use; .charCodeAt's out-of-range NaN is a separate, already-faithful behavior.

.normalize()

Not supported
normalize(form?: string): string

Unicode normalization (NFC/NFD/NFKC/NFKD). Deliberately deferred, not attempted — it needs real Unicode normalization tables this compiler has no infrastructure for; a fake identity-only implementation would silently mis-normalize any non-ASCII composed/decomposed text.

.match() / .matchAll()

Exact
match(re: string | RegExp): RegExpMatchArray | null; matchAll(re: RegExp): RegExpStringIterator<RegExpExecArray>

PCRE2-backed regex matching. .match() is real-JS-shaped, and .matchAll() is a RegExp String Iterator of exec results. See RegExp for the pattern-language caveats.

Returns

RegExpMatchArray | null | RegExpStringIterator<RegExpExecArray> — The match, or an iterator of matches.

.search(pattern)

supported
search(pattern: string | RegExp): number

Searches for a pattern and returns its index, or -1. A plain-string pattern is coerced to a RegExp exactly as in real JS (metacharacters are interpreted — `'a.b'.search('.')` is 0), and a RegExp pattern runs a real PCRE2 search.

Returns

number — First match index, or -1.

console.log('the quick brown fox'.search('brown'))   // 10
See it in a full example →

.replaceAll(from, to)

supported
replaceAll(from: string, to: string): string

Replaces every non-overlapping occurrence of `from` with `to`. An empty search is JS-faithful — `'abc'.replaceAll('', '-')` is `'-a-b-c-'`.

Returns

string — A new string with all matches replaced.

console.log('a-b-a-b'.replaceAll('-', ' '))   // a b a b
See it in a full example →

.localeCompare(other)

Works, with caveats
localeCompare(other: string): number

Compares two strings, returning exactly -1, 0, or 1. Binary-safe past an embedded NUL.

Returns

number — -1, 0, or 1.

console.log('apple'.localeCompare('banana'))   // -1
See it in a full example →
⚠ Differences from standard
  • Length-aware byte-order comparison, not real Unicode collation — no locale/Intl infrastructure.

String.fromCharCode(n)

Works, with caveats
String.fromCharCode(...codes: number[]): string

Builds a string from UTF-16 code units (each argument's ToUint16), stored as UTF-8; a high/low surrogate pair among the arguments is one character. Static method on String.

Returns

string — The constructed string.

console.log(String.fromCharCode(72, 105))   // Hi
See it in a full example →
⚠ Differences from standard
  • An unpaired surrogate is encoded on its own, so halves from two calls concatenated don't join into one character as in Node.

String.fromCodePoint(n)

Exact
String.fromCodePoint(...codes: number[]): string

Builds a string from code points, encoded as UTF-8; a value that is not an integer in [0, 0x10FFFF] throws RangeError: Invalid code point.

Returns

string — The constructed string.

console.log(String.fromCodePoint(84, 121, 112, 101, 71, 111))   // TypeGo
See it in a full example →

String.raw (tag and function)

supported
String.raw`template` / String.raw({ raw }, ...substitutions)

Interleaves the raw (undecoded) quasi text with the string-coerced interpolations — escape sequences appear verbatim (String.raw`a\nb` is a\nb), byte-for-byte the same as Node. Called as a function it takes any object whose raw is array-like, which is how a tag forwards its strings (String.raw(strings, ...values)).

console.log(String.raw`C:\path\to\file`)  // C:\path\to\file