Skip To Content

String Methods

.length #

Get the length of a string, spaces and all. Doesn't count just letters, it goes the inclusive route and counts them all!

let str = "Nightmare Bonnie hates the flashlight.";

// 38

.trim() #

Removes the whitespace at the beginning and end of a string. Not the white space between characters!

let str = "    Shine the light on Plushtrap. Be careful...      ",
trimmedStr = str.trim();

// "Shine the light on Plushtrap. Be careful..."

.includes() #

Returns a true or false value if a string contains a substring.

let str = "What was The Bite of 87?";

// true
console.log(str.includes("Golden Freddy"));
// false

.indexOf() #

Get the string index (starting from 0) of where a substring first occurs in a longer string. If the substring isn't there, it returns -1.

let alphabet = "abcdefg";

// 3
// 0
// -1

.toUpperCase() and .toLowerCase() #

These respective functions turn a string to all lowercase and uppercase letters. Pretty self-explanatory.

let lowercaseString = "was that breathing?!",
uppercaseString = "I HAVE TO LISTEN QUIETLY...";

// i have to listen quietly...;

There's no included method for capitalizing a string, but you can make a function to do this instead. Several string methods used here are explained further on.

let lowercaseString = "was that breathing?!";

function toCapitalizeString(string) {
let firstCharacter = string.charAt(0),
stringWithoutFirst = string.slice(1, -1);

return firstCharacter.toUpperCase() + stringWithoutFirst;

const capitalizedSentence = toCapitalizeString(lowercaseString);

// Was that breathing?!

.replace() #

Self-explanatory too, it replaces the first instance of a substring with another one. If you want to replace all instances, the string will need to be put within the global regex like this: /<substring>/b.

let str1 = "Nightmare Bonnie is down the hall!",
str2 = "Was that Nightmare Bonnie? It must have been Nightmare Bonnie!";

console.log(str1.replace('Bonnie', 'Chica'));
// Nightmare Chica is down the hall!
console.log(str2.replace('/Bonnie/g', 'Chica'));
// Was that Nightmare Bonnie? It must have been Nightmare Bonnie!

.slice() #

Slice takes a substring out of a string, cutting it out by specifying the starting and ending indexes.

let str = 'Nightmare Bonnie';

// Bonnie
console.log(str.slice(10, 13));
// Bon
console.log(str.slice(10, -1));
// Bonnie
console.log(str.slice(10, -3));
// Bon
// e

.split() #

Turns a string into an array by breaking it apart by the specificied substring. Using an empty string, or '', breaks the string apart by each character (including spaces).

let str = "Freddy Bonnie Chica Foxy";

console.log(str.split(' '));
// [ 'Freddy', 'Bonnie', 'Chica', 'Foxy' ]
// [ 'F', 'r', 'e', 'd', 'd', 'y', ' ', 'B', 'o', 'n', 'n', 'i', 'e', ' ', 'C', 'h', 'i', 'c', 'a', ' ', 'F', 'o', 'x', 'y' ]

.repeat() #

Takes a string and copies it a set number of times into a new string. Best used with .trim() to remove leftover white space.

let str = "What was that?! ";

// What was that?! What was that?! What was that?!

.match() #

Checks to see if a substring is matched in a string, and returns the matched string. The matches, one or more, are returned in an array that you can count.

Using a regex lets you look for multiple matches and see the specific ones in the array.

let str = "Was that Bonnie or Chica?! I think it was Chica...",
bonnieMatches = str.match(/Bonnie/g),
chicaMatches = str.match(/Chica/g),
capitalMatches = str.match(/[A-Z]/g);

// ["Bonnie"]
// ["Chica", "Chica"]
// ["W", "B", "C", "C"]

.charAt() #

Gets the string character at a specific index. Will only accept positive numbers, can't use negative ones to start from the end.

let str = "Freddy";

// F
console.log(str.charAt(str.length - 1));
// y