How To Remove The Last Instance Of A Letter From A String In JavaScript

Remove the Last Instance of a Letter from a String in JavaScript

In this article, you’ll learn how to remove the last instance of a letter from a string in JavaScript using a combination of the .getLastIndexOf() and either .substring() or .slice(); or using .split() and .join().

Remove the last instance of a letter from a string in JavaScript

Note: We’ll use an example of replacing the last comma of a list to demonstrate the solution. For example:

"LearnShareIT,ABBA,LMThong,123123asdf,"

Output:

"LearnShareIT,ABBA,LMThong,123123asdf"

The general method if you’re not using a Regular Expression (which will not be covered here) is by first getting the index of the letter’s last occurrence, then removing it by a function. 

For a refresher, the .getLastIndexOf() function (as the name implies) returns the index of the last occurrence of a specified letter or substring in a string; if nothing is found, the function returns -1.

After an index is found, a function is required to remove the letter. There are 2 general ways of doing this: Using the .substring() or .slice() function or the .split() and .join() functions.

However, it is required to check whether the .getLastIndexOf() function returns -1, as that index will not return the desired results.

Using the .substring() or .slice() functions

  • .substring(int start, int stop)/.slice(int start, int stop): Though they work differently in some specific cases, both return a shallow-copy (a copy that shares the same source as the original) substring starting from the start to stop parameters, excluding the letter at index stop.

With the function in mind, you would need to take the substring from index 0 (start=0) to the found index (stop=.getLastIndexOf()) then add that with the substring from the index after. Here’s an example:

const str = "LearnShareIT,ABBA,LMThong,123123asdf,";

function removeLastLetter(string, letter) {
	// Get the index of the last occurrence of the letter
	const index = string.lastIndexOf(letter);

	// If nothing is found, the original string is returned
	if (index == -1) {
		return string;
	}

	// Remove the letter
	return string.slice(0, index) + string.slice(index + 1);
}

console.log(removeLastLetter(str, ","));

Output:

"LearnShareIT,ABBA,LMThong,123123asdf"

Using the .split() or .join() functions

  • .split(string separator): Takes a string and returns an array containing every substring after being split up by the separator parameter.
  • .join(string separator): Takes an array and creates a string using the values of the array separated by the separator parameter.

At face value, these two doesn’t seem very helpful in this context; but if you split the string by an empty string (""), you’ll get an array containing every letter of the string. With this newly acquired array, you can replace the value at the last occurrence of the letter with an empty string. Finally, use the .join() function with its separator as an empty string to get the final string value. Here’s an example:

const str = "LearnShareIT,ABBA,LMThong,123123asdf,";

function removeLastLetter(string, letter) {
	// Get the index of the last occurrence of the letter
	const index = string.lastIndexOf(letter);

	// If nothing is found, the original string is returned
	if (index == -1) {
		return string;
	}

	// Split up the string into an array
	const arr = string.split("");

	// Replace the value at the index with a blank string ("")
	arr[index] = "";

	// Return a string after joining the array
	return arr.join("");
}

console.log(removeLastLetter(str, ","));

Output:

"LearnShareIT,ABBA,LMThong,123123asdf"

Note that this method has an issue when you’re dealing with extensive text, which would make the array be in the hundredths or thousandths in size.

Summary

To remove the last instance of a letter from a string in JavaScript, you first need to get the index of the last occurrence of the letter with the .getLastIndexOf() function; then remove it with either the .substring() or .slice() function or the .split() and .join() functions.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *