Tip by Stefan Trost | Last update on 2021-05-07 | Created on 2012-02-08
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).
Delete first and last character
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.
About the Author
You can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
PHP: Remove arbitrary Characters at the Beginning and the End of a String
Tutorial | 0 Comments
PHP: Check Strings with Ctype-Functions for Character Classes
Article | 0 Comments
Tutorial | 0 Comments
Tip | 0 Comments
Info | 0 Comments
PHP: Read out n-th Character from behind of String
Question | 1 Answer
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.
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
2019-01-06 at 00:42
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
"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
var strnew = strold.substr(0, strnew.length-1);
this can't work: strnew is undefined
2021-01-13 at 17:10
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