How To Remove The Last Word From A String Using JavaScript

Remove the Last Word from a String using JavaScript

With strings in Javascript, we have many ways to work with them through some built-in methods. In this article, I will remove the last word from a string using JavaScript with some available methods like split(), join(), slice(),..etc. You can familiarize with it through some examples below.

Remove the last word from a string using JavaScript

Use split() and join() method

Before implementing this method, we must understand how to use the two methods, split() and join().

split() method

Syntax:

string.split(separator, limit)

Parameters:

  • separator: is the string used to indicate the method to split the string here.
  • limit: is the limit number of elements of the array returned after splitting the string.

join() method

Syntax:

array.join(separator)

Parameter:

  • separator: A string used to concatenate the elements of an array.

In this method, split() separates each word from the string.

Example:

strArr = str.split(" ");

After separating the words from the string, we will remove the last word by removing yes from the array we created after running the split() method.

Next, we use the join() method to concatenate and output the string.

Example:

newStr = strArr.join(" ");

The code I used to remove the last word from a string with the use of the split() and the join() method is as follows:

function removeLastWord(str) {
	// Split string into an array containing words
	const strArr = str.split(" ");

	// Remove the last word
	strArr.pop();

	// Get new string
	return strArr.join(" ");
}

console.log(removeLastWord("Hello, i am LearnShareIT"));

Output:

Hello, I am

In this method, we will learn the operation and syntax of the two methods, split() and join() and use it to remove the last word from a string. In the example, I created a function to find and remove the last word using pop() to remove the element in the array.

Use lastIndexOf(), slice() and replace() method

To implement this method, you must also know the syntax and operation principle of the three methods lastIndexOf(), slice(), and replace().

lastIndexOf() method

Syntax:

string.lastIndexOf(searchValue, start)

Parameters:

  • searchValue: is the string that you want to search for it in the given string.
  • start: is the position to start the search.

slice() method

Syntax:

string.slice(start, end)

Parameters:

  • start: is the starting position to cut the string.
  • end: The end position of the cut string.

replace() method

Syntax:

string.replace(searchValue, newValue)

Parameters:

  • searchValue: is the string that you want to change to another string.
  • newValue: is the string you want to change to.

In this way, we only interact with the string, unlike the above, using the split() and join() method, which interacts and removes the last word through an array created from split().

I will use the lastIndexOf() and slice() methods to remove the last word:

function removeLastWord(str) {
	// Get the position of the last space
	const positionLastWord = str.lastIndexOf(" ");

	// Return string removed the last word
	return (str.replace(str.slice(positionLastWord), ""));
}

console.log(removeLastWord("Hello, i am LearnShareIT"));

Output:

Hello, I am

With this method, it is challenging to understand the step of getting the results to return, but this will help us learn more functions than using the split() and join() method suitable for those who are learning and want to learn more about JS.

Summary

I have helped you with some ways to remove the last word from a string using JavaScript. The above two methods are equivalent so that we can choose any method depending on ourselves. Let’s try both ways. Hope this article helps you and your program. Good luck.

Maybe you are interested:

Leave a Reply

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