String Methods

Strings come with dozens of built-in methods for searching, slicing, transforming and splitting text. You don't need to memorise them all — but knowing the dozen or so covered here will handle the vast majority of everyday text work.

Changing case and trimming

JS case-trim.js
const raw = "   Hello World   ";

console.log(`[${raw.toUpperCase()}]`);
console.log(`[${raw.toLowerCase()}]`);
console.log(`[${raw.trim()}]`);
console.log(`[${raw.trimStart()}]`);
console.log(`[${raw.trimEnd()}]`);
Output
[   HELLO WORLD   ]
[   hello world   ]
[Hello World]
[Hello World   ]
[   Hello World]

trim() is essential for user input, which often has stray spaces at either end.

Searching

JS search.js
const sentence = "The rain in Spain stays mainly in the plain";

console.log(sentence.includes("Spain"));
console.log(sentence.startsWith("The"));
console.log(sentence.endsWith("plain"));
console.log(sentence.indexOf("ain"));
console.log(sentence.lastIndexOf("ain"));
console.log(sentence.indexOf("snow"));
Output
true
true
true
5
40
-1

includes, startsWith and endsWith answer yes/no questions. indexOf returns the position of the first match, or -1 if there isn't one. All of them are case-sensitive.

Extracting parts: slice and substring

JS slice.js
const file = "holiday-photo.jpeg";

console.log(file.slice(0, 7));
console.log(file.slice(8));
console.log(file.slice(-4));
console.log(file.slice(file.lastIndexOf(".") + 1));
console.log(file.substring(0, 7));
Output
holiday
photo.jpeg
jpeg
jpeg
holiday

slice(start, end) returns the characters from start up to but not including end. Leave out end to go to the finish; use negative numbers to count from the end. substring is similar but doesn't accept negative positions — slice is the more flexible choice.

Replacing text

JS replace.js
const text = "cats and cats and cats";

console.log(text.replace("cats", "dogs"));
console.log(text.replaceAll("cats", "dogs"));
console.log("2026-10-06".replaceAll("-", "/"));
Output
dogs and cats and cats
dogs and dogs and dogs
2026/10/06

replace with a string changes only the first match; replaceAll changes every match. Both can also take a regular expression for pattern-based replacements (lesson 21).

Splitting and joining

split breaks a string into an array; the array method join does the reverse:

JS split.js
const csv = "Ana,Ben,Cleo";
const names = csv.split(",");
console.log(names);

console.log("hello".split(""));
console.log("one two  three".split(" "));
console.log(names.join(" | "));
Output
[ 'Ana', 'Ben', 'Cleo' ]
[ 'h', 'e', 'l', 'l', 'o' ]
[ 'one', 'two', '', 'three' ]
Ana | Ben | Cleo

Notice the empty string in the third result, caused by two spaces in a row. Splitting on a regular expression such as /\s+/ handles any amount of whitespace.

Padding and repeating

JS pad.js
console.log("7".padStart(3, "0"));
console.log("42".padEnd(6, ".") + "|");
console.log("ab".repeat(3));
console.log("-".repeat(20));
Output
007
42....|
ababab
--------------------

padStart is perfect for zero-padding numbers in times and IDs, and for lining up columns of text.

Characters and codes

JS chars.js
const word = "Hello";

console.log(word.charAt(1));
console.log(word.at(-2));
console.log(word.charCodeAt(0));
console.log(String.fromCharCode(72, 105));
Output
e
l
72
Hi

Chaining methods

Since each method returns a new string, you can chain them:

JS chain.js
const userInput = "   Hello, WORLD!   ";

const slug = userInput
  .trim()
  .toLowerCase()
  .replaceAll(",", "")
  .replaceAll("!", "")
  .split(" ")
  .join("-");

console.log(slug);
Output
hello-world

Quick reference

MethodReturns
toUpperCase() / toLowerCase()Case-changed copy
trim(), trimStart(), trimEnd()Copy without surrounding whitespace
includes(), startsWith(), endsWith()true / false
indexOf(), lastIndexOf()Position, or -1
slice(start, end)Part of the string
replace(), replaceAll()Copy with replacements
split(separator)Array of pieces
padStart(), padEnd(), repeat()Padded or repeated copy
at(i), charAt(i)One character

Try it yourself

Try it yourself
Console output