57 Votes

JavaScript: Remove last character from string

Tip by Stefan Trost | Last update on 2021-05-07 | Created on 2012-02-08

In this article I want to show you how you can remove the last character from any string with the help of JavaScript.

Before I start with the explanations, let's first take a look at the code required for this. Here is how to do it:

var strold = '1,2,3,';
var strnew = strold.substr(0, strold.length - 1);
alert(strnew);  // output: '1,2,3'

In the example, we have specified a string "strold", which contains a comma too much at its end, that we want to remove. The new resulting string should be later stored in the variable "strnew".

We are using the method substr(), which allows us to return parts of a string. Substr() takes two parameters: The first parameter is the position of the start (0 for the first character) and the second parameter is the length of our substring (number of characters).

In order to delete the last character, we proceed as follows: We want to extract a substring starting from the first character of the original string. So our first parameter is 0, because in JavaScript, the first character is the 0th character. From this character on, we want to keep as many characters as the string is long up to one character at the end. So, we can get the length of the old string with "strold.length" and simply subtract 1.

Delete first and last character

To illustrate this, here is just another example in which we want to delete the first and the last character from a string in JavaScript:

var strold = '12345';
var strnew = strold.substr(1, strold.length - 2);
alert(strnew);  // output: '234'

If we also do not want to keep the first character of the string, we have to start at 1 rather than at 0 (the first character is 0, the second character is 1). Thus, our first parameter is 1, so that we can cut the first letter off the string.

From this character on, we want to keep as many characters as the string length minus 2. If we would subtract 1 again as in the last example, we would keep our last character here, because this time, we are starting one character from the beginning. The second parameter is therefore not the position up to which we want to cut, but the number of characters counted from the position that was specified as the first parameter.

ReplyPositiveNegativeDateVotes
00 Votes

substr() is a Javascript string method, not—strictly speaking—a function. Otherwise this is an excellent tip.
2019-01-06 at 00:42

ReplyPositive Negative
22 Votes

Thank you very much for that hint, I have corrected it in my article.

Perhaps you can create an own article, explaining the difference between functions and methods. That would be very interesting.
2019-01-06 at 19:59

Positive Negative
-11 Vote

"Perhaps you can create an own article, explaining the difference between functions and methods."

Ha he ha! That's the way you gotta talk to this generation of snowflake coders now. The oop sheep don't know there's only two things that exist in a programing language: variables and functions. The inmates are running the asylum.
2019-07-05 at 19:03

Positive Negative
Reply
00 Votes

Hello,

var strnew = strold.substr(0, strnew.length-1);

this can't work: strnew is undefined
2021-01-13 at 17:10

ReplyPositive Negative
00 Votes

Of course, you are right. It must be strold like in the second example, I have corrected that in my article.

Thanks for reporting that error.
2021-01-13 at 17:14

Positive Negative
Reply
Reply

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.