String methods
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.
+ (concatenation)
ExactJoins 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
ExactThe 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?)
ExactReturns 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.
string — A new string spanning [start, end).
console.log('Hello, World!'.slice(7, 12)) // World
console.log('Hello, World!'.slice(-6)) // orld!.substring(start, end?)
ExactLike 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.
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).substr(start, length?)
ExactDeprecated 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.
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!.indexOf(substr, fromIndex?)
ExactReturns 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.
Returnsnumber — First match index, or -1.
console.log('the quick brown fox'.indexOf('brown')) // 10.lastIndexOf(substr, fromIndex?)
Works, with caveatsReturns 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.
Returnsnumber — Last match index, or -1.
console.log('path/to/file'.lastIndexOf('/')) // 7- Returns a byte offset, not a UTF-16 index — like
.indexOfon non-ASCII text.
.includes(substr, position?)
ExactWhether the string contains the substring, searching from the optional position. Binary-safe (searches with memmem over a length header).
Returnsboolean — true if the substring is present.
console.log('report.pdf'.includes('.pdf')) // true.startsWith(prefix, position?)
ExactWhether the string begins with the prefix at the optional position (default 0). Returns false when the string is shorter than the prefix.
Returnsboolean — true if the string starts with the prefix.
console.log('https://example.com'.startsWith('https')) // true.endsWith(suffix, endPosition?)
ExactWhether 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.
Returnsboolean — true if the string ends with the suffix.
console.log('report.pdf'.endsWith('.pdf')) // true.replace(from, to)
ExactReplaces the first occurrence of `from` with `to`, returning the original string when there is no match. See RegExp for regex-pattern edges.
Returnsstring — A new string with the first match replaced.
console.log('Hello World'.replace('World', 'TypeGo')) // Hello TypeGo.split(sep, limit?)
ExactSplits 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.
Returnsstring[] — 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()
ExactStrips 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.
Returnsstring — The trimmed string.
console.log(' hello world '.trim()) // hello world.trimStart() / .trimEnd()
ExactStrips whitespace from one side only, using the same full whitespace set as trim().
Returnsstring — The one-sided-trimmed string.
console.log('[' + ' hi '.trimStart() + ']') // [hi ].toString()
ExactIdentity 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).
Returnsstring — The same string.
.toUpperCase()
ExactReturns 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.
Returnsstring — The uppercased string.
console.log('straße'.toUpperCase()) // STRASSE.toLowerCase()
ExactReturns the string lowercased by the Unicode Default Case Conversion, as Node — including the context-sensitive final sigma ('ΟΔΥΣΣΕΥΣ' becomes 'οδυσσευς') and the 'İ' expansion.
Returnsstring — The lowercased string.
console.log('ΟΔΥΣΣΕΥΣ'.toLowerCase()) // οδυσσευς.repeat(n)
ExactReturns the string repeated n times.
Returnsstring — The repeated string.
console.log('ab'.repeat(3)) // ababab.padStart(len, pad?)
ExactPads 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.
string — The padded string.
console.log('7'.padStart(3, '0')) // 007.padEnd(len, pad?)
ExactPads 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.
string — The padded string.
console.log('7'.padEnd(3, '.')) // 7...concat(...values)
ExactReturns 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.
string — The concatenated string.
console.log('a'.concat('b', 'c')) // abc
console.log('s:'.concat(...['p', 'q'])) // s:pq.charCodeAt(i)
ExactReturns 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.
Returnsnumber — The code unit, or NaN if out of range.
console.log('Hello'.charCodeAt(0)) // 72.at(i)
ExactReturns the character at index i, with negative indices counting from the end; an out-of-range index is a real undefined, as in Node.
Returnsstring | undefined — The character at i, or undefined if i is out of range.
.charAt(i)
ExactReturns 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().
Returnsstring — The character at i, or "" if out of range.
.codePointAt(i)
Works, with caveatsReturns the code point at index i, or undefined if out of range.
Returnsnumber | 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- 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 isnumber | undefined— narrow,?? n, or!before use;.charCodeAt's out-of-rangeNaNis a separate, already-faithful behavior.
.normalize()
Not supportedUnicode 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()
ExactPCRE2-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.
ReturnsRegExpMatchArray | null | RegExpStringIterator<RegExpExecArray> — The match, or an iterator of matches.
.search(pattern)
supportedSearches 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.
Returnsnumber — First match index, or -1.
console.log('the quick brown fox'.search('brown')) // 10.replaceAll(from, to)
supportedReplaces every non-overlapping occurrence of `from` with `to`. An empty search is JS-faithful — `'abc'.replaceAll('', '-')` is `'-a-b-c-'`.
Returnsstring — A new string with all matches replaced.
console.log('a-b-a-b'.replaceAll('-', ' ')) // a b a b.localeCompare(other)
Works, with caveatsCompares two strings, returning exactly -1, 0, or 1. Binary-safe past an embedded NUL.
Returnsnumber — -1, 0, or 1.
console.log('apple'.localeCompare('banana')) // -1- Length-aware byte-order comparison, not real Unicode collation — no locale/
Intlinfrastructure.
String.fromCharCode(n)
Works, with caveatsBuilds 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.
Returnsstring — The constructed string.
console.log(String.fromCharCode(72, 105)) // Hi- 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)
ExactBuilds a string from code points, encoded as UTF-8; a value that is not an integer in [0, 0x10FFFF] throws RangeError: Invalid code point.
Returnsstring — The constructed string.
console.log(String.fromCodePoint(84, 121, 112, 101, 71, 111)) // TypeGoString.raw (tag and function)
supportedInterleaves 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