Published on: 01/04/2024
Written by James Bridge
Below are a range of useful string operations available in JavaScript
Combine two or more strings.
let greeting = "Hello, ";
let name = "World!";
console.log(greeting + name); // "Hello, World!"
Find out how many characters are in a string
let text = "Hello, World!";
console.log(text.length); // 13
Find the first index number for a substring
let text = "Hello, World!";
console.log(text.indexOf("World")); // 7
Find the last index number for a substring
let text = "Hello, World, Hello!";
console.log(text.lastIndexOf("Hello")); // 13
Extracts a section of a string from startIndex to, but not including, endIndex. If endIndex is not provided, it extracts till the end of the string. If endIndex is negative, it’s treated as strLength + endIndex where strLength is the string’s length.
let text = "Hello, World!";
console.log(text.slice(7, 12)); // "World"
Extracts characters from startIndex up to, but not including, endIndex. If endIndex is not specified, extraction goes to the end of the string. Unlike slice(), if endIndex is less than startIndex, substring will swap the two arguments; slice will not.
let text = "Hello, World!";
console.log(text.substring(7, 12)); // "World"
Compares two strings in the current locale.
console.log("a".localeCompare("b")); // -1
console.log("a".localeCompare("a")); // 0
console.log("b".localeCompare("a")); // 1
Converts a string to lowercase letters.
let text = "Hello, World!";
console.log(text.toLowerCase()); // "hello, world!"
Converts a string to uppercase letters.
let text = "Hello, World!";
console.log(text.toUpperCase()); // "HELLO, WORLD!"
let text = " Hello, World! ";
console.log(text.trim()); // "Hello, World!"
Retrieves the result of matching a string against a regular expression.
let text = "The rain in SPAIN stays mainly in the plain";
console.log(text.match(/ain/g)); // ["ain", "ain", "ain"]
Returns an iterator of all results matching a string against a regular expression.
let text = "The rain in SPAIN stays mainly in the plain";
let regex = /ain/g;
console.log(Array.from(text.matchAll(regex))); // [Array with matches]
Determines whether a string begins with the characters of a specified string.
let text = "Hello, World!";
console.log(text.startsWith("Hello")); // true
Determines whether a string ends with the characters of a specified string.
let text = "Hello, World!";
console.log(text.endsWith("World!")); // true
Determines whether one string may be found within another string.
let text = "Hello, World!";
console.log(text.includes("World")); // true
Replaces occurrences of a substring with another substring in a string.
let text = "Hello, World!";
console.log(text.replace("World", "JavaScript")); // "Hello, JavaScript!"
Replaces all occurrences of a substring with another substring in a string.
let text = "Hello, World! World is great.";
console.log(text.replaceAll("World", "JavaScript")); // "Hello, JavaScript! JavaScript is great."
Splits a string into an array of substrings based on a specified separator.
let text = "Hello, World! How are you?";
console.log(text.split(" ")); // ["Hello,", "World!", "How", "are", "you?"]
console.log(text.split(", ")); // ["Hello", "World! How are you?"]
console.log(text.split("")); // Splits every character into an array