I have a string like
count-contribute-1
count-contribute-11
count-contribute-1111
Here I want to split the string and get the last split value (i.e 1
, 11
, 1111
);
How can I do it?
I have a string like
count-contribute-1
count-contribute-11
count-contribute-1111
Here I want to split the string and get the last split value (i.e 1
, 11
, 1111
);
How can I do it?
Use .pop() to get the last item from the array created by .split()
"count-contribute-1".split('-').pop();
Also you can get last part of numbers using regular expression. Like this:
s = "count-contribute-111"
s.match(/\d+$/)
//return "111"
It doesn't matter what separator you use.
s = "count-contribute-+*%111"
s.match(/\d+$/)
//return "111"