Home » Programming » Javascript » Javascript Remove First Last Character String

JavaScript: Remove the First/Last Character from a String [Examples]

This tutorial will explain a few ways to remove the first or last character (or both!) from a string in the JavaScript programming language.

This can be useful if you need to capitalize names or sentences or remove punctuation.

Remove First/Last Character Using The substring() Method

The JavaScript substring() method can be used to remove the first or last character from a string.

You can find out more about the substring() and similar substr() methods here.

The below code example shows how to remove the first, last, and both the first and last characters from a string.

var myString = "Hello LinuxScrew!"; // Define a string
var removeFirstLetter = myString.substring(1); // Will return "ello LinuxScrew!"
var removeLastLetter = myString.substring(0, myString.length -1); // Will return "Hello LinuxScrew"
var removeFirstAndLastLetter = myString.substring(1, myString.length -1); // Will return "ello LinuxScrew"

What if the String is Only 1 Character Long?

Using substring() has one drawback – it doesn’t play well with single-character strings. If you aren’t sure how long the string you will be processing is in advance or know that it could take a single-character value, it’s recommended to use the slice() method outlined later in this tutorial.

var myString = "H"; // Define a string
var removeFirstLetter = myString.substring(1); // Will return ""
var removeLastLetter = myString.substring(0, myString.length -1); //  Will return ""
var removeFirstAndLastLetter = myString.substring(1, myString.length -1); // Will return "H" - which is wrong!

Why doesn’t this method work when only a single character is present? It’s to do with how substring() works. substring() uses the index of the characters in the string to determine what is kept and what is discarded.

What About an Empty String?

The substring() method does behave correctly when dealing with empty strings – returning the expected empty string when attempting to remove first/last characters:

var myString = ""; // Define a string
var removeFirstLetter = myString.substring(1); // Will return ""
var removeLastLetter = myString.substring(0, myString.length -1); //  Will return ""
var removeFirstAndLastLetter = myString.substring(1, myString.length -1); // Will return ""

Remove First/Last Character Using the slice() Method

If you aren’t sure what length the string you wish to work on will be, it’s safer to use the slice() method:

var myString = "Hello LinuxScrew!"; // Define a string
var removeFirstLetter = myString.slice(1, myString.length); // Will return "ello LinuxScrew!"
var removeLastLetter = myString.slice(0, s.length -1); // Will return "Hello LinuxScrew"
var removeFirstAndLastLetter = myString.slice(1,myString.length).slice(0, myString.length - 2); // Will return "ello LinuxScrew"

Notice on the last line that the second call to slice() has an end position of myString.length – 2 – because two characters are being removed in two separate slice operations.

Using slice() will work regardless of the length of the string.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment