Skip to main content
Porffor implements the full String API for string manipulation and processing.

String Methods

charAt

Returns the character at the specified index.
string.charAt(index: number): string
const str = 'Hello';
console.log(str.charAt(0));  // 'H'
console.log(str.charAt(4));  // 'o'
console.log(str.charAt(10)); // '' (empty string)

charCodeAt

Returns the Unicode value of the character at the specified index.
string.charCodeAt(index: number): number
const str = 'ABC';
console.log(str.charCodeAt(0)); // 65 (A)
console.log(str.charCodeAt(1)); // 66 (B)
console.log(str.charCodeAt(2)); // 67 (C)

concat

Concatenates strings.
string.concat(...strings: string[]): string
const str1 = 'Hello';
const str2 = str1.concat(' ', 'World', '!');
console.log(str2); // 'Hello World!'

indexOf

Returns the first index of a substring, or -1 if not found.
string.indexOf(searchString: string, position?: number): number
const str = 'Hello World';
console.log(str.indexOf('o'));      // 4
console.log(str.indexOf('o', 5));   // 7
console.log(str.indexOf('xyz'));    // -1

lastIndexOf

Returns the last index of a substring.
string.lastIndexOf(searchString: string, position?: number): number
const str = 'Hello World';
console.log(str.lastIndexOf('o'));  // 7
console.log(str.lastIndexOf('l'));  // 9

slice

Extracts a section of a string.
string.slice(start: number, end?: number): string
const str = 'Hello World';
console.log(str.slice(0, 5));   // 'Hello'
console.log(str.slice(6));      // 'World'
console.log(str.slice(-5));     // 'World'
console.log(str.slice(0, -6));  // 'Hello'

substring

Returns a substring between two indices.
string.substring(start: number, end?: number): string
const str = 'Hello World';
console.log(str.substring(0, 5));  // 'Hello'
console.log(str.substring(6));     // 'World'
console.log(str.substring(6, 11)); // 'World'

substr

Returns a substring starting at a position with specified length.
string.substr(start: number, length?: number): string
const str = 'Hello World';
console.log(str.substr(0, 5));  // 'Hello'
console.log(str.substr(6, 5));  // 'World'
console.log(str.substr(-5, 5)); // 'World'

toLowerCase

Converts string to lowercase.
string.toLowerCase(): string
const str = 'Hello World';
console.log(str.toLowerCase()); // 'hello world'

toUpperCase

Converts string to uppercase.
string.toUpperCase(): string
const str = 'Hello World';
console.log(str.toUpperCase()); // 'HELLO WORLD'

trim

Removes whitespace from both ends.
string.trim(): string
const str = '  Hello World  ';
console.log(str.trim()); // 'Hello World'

trimStart / trimLeft

Removes whitespace from the start.
string.trimStart(): string
const str = '  Hello World  ';
console.log(str.trimStart()); // 'Hello World  '

trimEnd / trimRight

Removes whitespace from the end.
string.trimEnd(): string
const str = '  Hello World  ';
console.log(str.trimEnd()); // '  Hello World'

split

Splits string into an array of substrings.
string.split(separator?: string | RegExp, limit?: number): string[]
const str = 'a,b,c,d';
console.log(str.split(','));      // ['a', 'b', 'c', 'd']
console.log(str.split(',', 2));   // ['a', 'b']
console.log('hello'.split(''));   // ['h', 'e', 'l', 'l', 'o']

replace

Replaces occurrences of a substring or pattern.
string.replace(searchValue: string | RegExp, replaceValue: string): string
const str = 'Hello World';
console.log(str.replace('World', 'Porffor')); // 'Hello Porffor'
console.log(str.replace(/o/g, '0'));          // 'Hell0 W0rld'

replaceAll

Replaces all occurrences of a substring.
string.replaceAll(searchValue: string | RegExp, replaceValue: string): string
const str = 'aabbcc';
console.log(str.replaceAll('b', 'x')); // 'aaxxcc'

match

Matches a string against a regular expression.
string.match(regexp: RegExp): string[] | null
const str = 'The year is 2024';
const matches = str.match(/\d+/);
console.log(matches); // ['2024']

const globalMatches = 'a1 b2 c3'.match(/\d/g);
console.log(globalMatches); // ['1', '2', '3']
Searches for a match and returns the index.
string.search(regexp: RegExp): number
const str = 'Hello World';
console.log(str.search(/World/));  // 6
console.log(str.search(/xyz/));    // -1

startsWith

Checks if string starts with specified substring.
string.startsWith(searchString: string, position?: number): boolean
const str = 'Hello World';
console.log(str.startsWith('Hello'));     // true
console.log(str.startsWith('World'));     // false
console.log(str.startsWith('World', 6));  // true

endsWith

Checks if string ends with specified substring.
string.endsWith(searchString: string, length?: number): boolean
const str = 'Hello World';
console.log(str.endsWith('World'));    // true
console.log(str.endsWith('Hello'));    // false
console.log(str.endsWith('Hello', 5)); // true

includes

Checks if string contains a substring.
string.includes(searchString: string, position?: number): boolean
const str = 'Hello World';
console.log(str.includes('World'));    // true
console.log(str.includes('xyz'));      // false
console.log(str.includes('World', 7)); // false

repeat

Repeats the string a specified number of times.
string.repeat(count: number): string
console.log('abc'.repeat(3));   // 'abcabcabc'
console.log('*'.repeat(5));     // '*****'

padStart

Pads the string from the start.
string.padStart(targetLength: number, padString?: string): string
const str = '5';
console.log(str.padStart(3, '0'));   // '005'
console.log('abc'.padStart(10, '*')); // '*******abc'

padEnd

Pads the string from the end.
string.padEnd(targetLength: number, padString?: string): string
const str = '5';
console.log(str.padEnd(3, '0'));     // '500'
console.log('abc'.padEnd(10, '*'));  // 'abc*******'

at

Returns the character at an index (supports negative indexing).
string.at(index: number): string | undefined
const str = 'Hello';
console.log(str.at(0));   // 'H'
console.log(str.at(-1));  // 'o'
console.log(str.at(-2));  // 'l'

Static Methods

String.fromCharCode

Creates a string from character codes.
String.fromCharCode(...codes: number[]): string
console.log(String.fromCharCode(72, 101, 108, 108, 111)); // 'Hello'
console.log(String.fromCharCode(65, 66, 67));             // 'ABC'

Usage Examples

Text Processing

function normalizeWhitespace(text) {
  return text.trim().replace(/\s+/g, ' ');
}

console.log(normalizeWhitespace('  hello    world  '));
// 'hello world'

String Validation

function isValidEmail(email) {
  return email.includes('@') && 
         email.indexOf('@') > 0 && 
         email.indexOf('@') < email.length - 1;
}

console.log(isValidEmail('user@example.com')); // true
console.log(isValidEmail('invalid'));          // false

Case Conversion

function toTitleCase(str) {
  return str.toLowerCase()
    .split(' ')
    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ');
}

console.log(toTitleCase('hello world')); // 'Hello World'

See Also

Build docs developers (and LLMs) love