I have Show
Using JavaScript, how might I get the last five characters or last character?
Uwe Keim 38.7k56 gold badges173 silver badges282 bronze badges asked May 3, 2011 at 18:12
3 EDIT: As others have pointed out, use Original answer: You'll want to use the Javascript string method
This gets the characters starting at id.length - 5 and, since the second argument for .substr() is omitted, continues to the end of the string. You can also use the If you're simply looking to find the characters after the underscore, you could use this:
This splits the string into an array on the underscore and then "pops" the last element off the array (which is the string you want). answered May 3, 2011 at 18:19
Jamon HolmgrenJamon Holmgren 22.8k6 gold badges57 silver badges75 bronze badges 11
Don't use the deprecated Use either the The are slight differences in requirements, which are properly documented on: String.prototype.substring()
answered Mar 18, 2013 at 9:41
TerenceTerence 9,8151 gold badge14 silver badges20 bronze badges 10 You can use the substr() method with a negative starting position to retrieve the last n characters. For example, this gets the last 5:
Mogsdad 43.4k20 gold badges147 silver badges263 bronze badges answered Jun 18, 2015 at 13:25
user682701user682701 1,4011 gold badge9 silver badges4 bronze badges 5 Getting the last character is easy, as you can treat strings as an array:
To get a section of a string, you can use the substr function or the substring function:
The difference between answered May 3, 2011 at 18:21
ZirakZirak 37.9k12 gold badges78 silver badges90 bronze badges 1 Following script shows the result for get last 5 characters and last 1 character in a string using JavaScript:
Output :
answered Apr 18, 2012 at 4:52
tamiltamil 3413 silver badges2 bronze badges 1
results '3c58'
results '8'
answered Oct 30, 2021 at 8:51
user769371user769371 2773 silver badges9 bronze badges 1 One way would be using
so the value of answered May 17, 2018 at 15:26
KeivanKeivan 1,0541 gold badge14 silver badges26 bronze badges Check out the substring function. To get the last character:
answered May 3, 2011 at 18:15
ChewpersChewpers 2,4024 gold badges22 silver badges30 bronze badges 0 The Substr function allows you to use a minus to get the last character.
It's very flexible. For example:
answered Apr 1, 2019 at 11:08
There is no need to use taking the example of Jamon Holmgren we can change substr method and simply specify the array position:
answered Jul 5, 2013 at 12:55
Nello OllenNello Ollen 3275 silver badges22 bronze badges PerformanceToday 2020.12.31 I perform tests on MacOs HighSierra 10.13.6 on Chrome v87, Safari v13.1.2 and Firefox v84 for chosen solutions for getting last N characters case (last letter case result, for clarity I present in separate answer). ResultsFor all browsers
DetailsI perform 2 tests cases:
Below snippet presents solutions A B C D E F G (my) And here are example results for chrome answered Dec 31, 2020 at 0:18
Kamil KiełczewskiKamil Kiełczewski 75.7k26 gold badges335 silver badges311 bronze badges If you just want the last character or any character at know position you can simply trat string as an array! - strings are iteratorable in javascript -
Yet if you need more than just one character then use splice is effective
Regarding your example
To extract id you can easily use split + pop
This will return the id, or undefined if no id was after 'rating_element' :) answered Nov 17, 2017 at 22:47
ZalabozaZalaboza 8,73916 gold badges75 silver badges136 bronze badges PerformanceToday 2020.12.31 I perform tests on MacOs HighSierra 10.13.6 on Chrome v87, Safari v13.1.2 and Firefox v84 for chosen solutions for getting last character case (last N letters case results, for clarity I present in separate answer). ResultsFor all browsers
DetailsI perform 2 tests cases:
Below snippet presents solutions A B C D E F G (my), H (my) And here are example results for chrome answered Dec 31, 2020 at 0:11
Kamil KiełczewskiKamil Kiełczewski 75.7k26 gold badges335 silver badges311 bronze badges
Docs: String/at answered Mar 21, 2021 at 17:37
t_dom93t_dom93 8,4821 gold badge46 silver badges34 bronze badges
I found this question and through some research I found this to be the easiest way to get the last character. As others have mentioned and added for completeness to get the last 5:
answered May 16, 2018 at 15:07
You can exploit the
Similarly, you can use answered Apr 11, 2021 at 13:44
arbobarbob 1511 silver badge5 bronze badges Assuming you will compare the substring against the end of another string and use the result as a boolean you may extend the String class to accomplish this:
Allowing you to do the following:
answered Jan 24, 2016 at 0:51
mikeborghmikeborgh 1,18411 silver badges21 bronze badges 1 To get the last character of a string, you can use the
It's works because when the
Arghya Sadhu 37k9 gold badges67 silver badges91 bronze badges answered Aug 22, 2020 at 14:38
Jonas BragaJonas Braga 1313 silver badges3 bronze badges I actually have the following problem and this how i solved it by the help of above answer but different approach in extracting id form a an input element. I have attached input filed with an
And , when that button clicked i want to extract the id(which is the number) or the php ID ($id) only. So here what i do .
answered Jun 8, 2015 at 10:34
Daniel AdenewDaniel Adenew 7,3237 gold badges54 silver badges75 bronze badges This one will remove the comma if it is the last character in the string..
Code Lღver 15.5k16 gold badges54 silver badges74 bronze badges answered Aug 30, 2013 at 6:00
1 Last 5
Last
answered Apr 1, 2019 at 11:16
rajmobiapprajmobiapp 9298 silver badges10 bronze badges 1 you can use slice id.slice(-5); answered May 10 at 9:17
I am sure this will work....
The value of answered Sep 4, 2018 at 15:05
Here 2 examples that will show you always the last character
answered Sep 17, 2020 at 16:03
How do I find the last 4 digits of a string?If data is in not in string form, use String.. String lastFourDigits = "" ; //substring containing last 4 characters.. if (input.length() > 4 ) {. lastFourDigits = input.substring(input.length() - 4 ); }. else. {. lastFourDigits = input; }. System. out. println(lastFourDigits);. How do I get the last 5 characters of a string?To get the last N characters of a string, call the slice method on the string, passing in -n as a parameter, e.g. str. slice(-3) returns a new string containing the last 3 characters of the original string. Copied! const str = 'Hello World'; const last3 = str.
How do you find the last digit of a string?Method 1: Using String.. The idea is to use charAt() method of String class to find the first and last character in a string.. The charAt() method accepts a parameter as an index of the character to be returned.. How do I get the last character in a JavaScript string?To get the last character of a string, call the charAt() method on the string, passing it the last index as a parameter. For example, str. charAt(str. length - 1) returns a new string containing the last character of the string.
|