;

How to Remove the Last Word from the String Using JavaScript


Tutorialsrack 11/06/2020 Jquery Javascript

In this article, you will learn how to remove the last word from the string using Javascript. There are many ways to remove the last word in the string using javascript.

Here are the various examples for removing the last word in the string using javascript.

How to Remove the First Character From a String in Javascript

How to Remove Last Character From String Using Javascript or JQuery

How to Remove the First and Last Character from a String using Javascript

Example 1: Using string.substring() Function 

In this example, we used the string.substring() function and string.lastIndexOf() function to remove the last word from the string. The string.lastIndexOf() function is used to get the last index of the specific character in the string. And the string.substring() function is used to extract the characters from a string, between two specified indices, and returns the new substring.

Here is the source code of the program to remove the last word from the string using the string.substring() function in javascript.

Example 1: Using string.substring() Function
var str = "I want to remove the last word.";

// Get the Last index of Space 
// or any specified character in place of space
var index = str.lastIndexOf(" ");

// returns the string from 0 to specified index
str = str.substring(0, index);

console.log(str)
//Output ==> "I want to remove the last"

Example 2: Using split(), pop() and join() Function

In this example, we used the string.split() function to split the string into the array, the array.pop() function is used to remove the last element of an array and returns that element. And the array.join() function returns the array as a string.

Here is the source code of the program to remove the last word in the string using split(), pop() and join() function in javascript.

Example 2: Using split(), pop() and join() Function
var str = "I want to remove the last word.";

//split by space 
//or any specified character in place of space
var res = str.split(" "); 

//remove last element
res.pop(); 

//join back together
console.log(res.join(" ")); 
//Output ==> "I want to remove the last"

Example 3: Using split(), splice() and join() Function

In this example, we used the string.split() function to split the string into words and the splice() function adds/removes items to/from an array and returns the removed item(s) and the join() function returns the array as a string.

Here is the source code of the program to remove the last word in the string using split(), splice() and join() function in javascript.

Example 3: Using split(), splice() and join() Function
var str = "I want to remove the last word.";
var newStr = str.split(" ").slice(0, -1).join(" ");

console.log(newStr);
//Output ==> "I want to remove the last"

I hope this article will help you to understand how to remove the last word from the string in Javascript.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags