Remove last character from string in JS [5 Methods]


Written by - Olorunfemi Akinlua
Reviewed by - Deepak Prasad

Strings are immutable and therefore can’t be manipulated. So how then can we remove the last character from a string? Here is where the full definition of a string comes to play - it is a sequence of characters. Given that it is a sequence of character, we can access each character element within the string. With this, we can create a new string where we only select the character element we want to deal with immutability.

Therefore, to remove last character from string, we can select all the character element of a string aside from the last character and create a new string binding. This will not affect the main string itself as it is immutable, but rather a create a copy string without the last character.

In this article, we will discuss the different ways we can remove the last character from a string

 

Different methods to remove last character from string in JavaScript

Here are some possible methods you can use to remove the last character from a JavaScript string:

  • Using the slice() method: You can use the slice() method to create a new string that excludes the last character of the original string.
  • Using the substring() method: Similar to the slice() method, you can use the substring() method to create a new string that excludes the last character of the original string.
  • Using the substr() method: The substr() method can be used to create a new string that excludes the last character of the original string by specifying the start index and length of the new string.
  • Using the replace() method: You can use the replace() method to replace the last character of the original string with an empty string.
  • Using the split() and join() methods: You can split the string into an array of characters using the split() method, remove the last element of the array using pop(), and then join the remaining characters back into a string using the join() method.
  • Using regular expressions: Regular expressions can also be used to remove the last character of a string by matching and replacing the last character with an empty string.

 

Method-1: Using slice()

The slice() method is a built-in method in JavaScript that returns a portion of a string starting from the specified index. To remove the last character from a string using slice(), we can specify a negative value for the end index.

const str = "Welcome to the Blog";
const newStr = str.slice(0, -1);
console.log(newStr);

Output

Welcome to the Blo

In the above example, we used the slice() method to create a new string that excludes the last character of the original string. The slice() method takes two arguments - the start index and the end index. In this case, we passed -1 as the end index, which means the method returns the string up to the second to last character.

 

Method-2: Using substring()

The substring() method is similar to the slice() method in that it returns a portion of a string. However, it takes two arguments - the start index and the end index - both of which are positive integers. To remove the last character from a string using substring(), we can pass the length of the string minus one as the end index.

const str = "GoLinux is the best learning platform";
const newStr = str.substring(0, str.length - 1);
console.log(newStr);

Output

GoLinux is the best learning platfor

In the above example, we used the substring() method to create a new string that excludes the last character of the original string. We passed str.length - 1 as the end index, which means the method returns the string up to the second to last character.

 

Method-3: Using substr()

The substr() method is similar to the substring() method in that it returns a portion of a string. However, it takes two arguments - the start index and the length of the substring. To remove the last character from a string using substr(), we can pass the length of the string minus one as the length argument.

const str = "Substr method is becoming obselete";
const newStr = str.substr(0, str.length - 1);
console.log(newStr);

Output

Substr method is becoming obselet

In the above example, we used the substr() method to create a new string that excludes the last character of the original string. We passed str.length - 1 as the length argument, which means the method returns a substring that starts at index 0 and has a length of str.length - 1.

However, this method is no longer recommended, but can be useful with some browsers.

 

Method-4: Using RegExp and replace()

The replace() method is used to replace a portion of a string with another string. By using RegExp, we can replace the last character of a string with an empty string.

const str = "Wow, Regular Expression is cool";
const newStr = str.replace(/.$/, "");
console.log(newStr);

Output

Wow, Regular Expression is coo

In the above example, we used the replace() method with a regular expression to remove the last character of the string. The regular expression /$/ matches the end of the string, and . matches any character. Therefore, .$ matches the last character of the string, which we replace with an empty string.

 

Method-5: Using the split() and join()

Here's an example of how to remove the last character from a JavaScript string using the split() and join() methods:

let myString = "Hello World!";
let newString = myString.split('');
newString.pop();
newString = newString.join('');
console.log(newString);

In this example, the split() method is used to split the original string "Hello World!" into an array of individual characters. The pop() method is then used to remove the last character of the array, which is the exclamation mark.

Finally, the join() method is used to join the remaining characters back into a string. The result is a new string that excludes the last character of the original string, which is outputted using console.log().

"Hello World"

 

Summary

With the above approaches - slice(), substring(), substr(), and replace() - we can remove the last character from a string in JavaScript with ease. The use of regular expression can give us more control on what character we want to remove. If we decide that we want to remove a last character that’s a number, we can do that with the replace() method, making it more versatile.

 

References

String.prototype.slice() - JavaScript | MDN (mozilla.org)
String.prototype.substring() - JavaScript | MDN (mozilla.org)
String.prototype.substr() - JavaScript | MDN (mozilla.org)
String.prototype.replace() - JavaScript | MDN (mozilla.org)

 

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on LinkedIn.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment