Timeline for How to get the last character of a string?
Current License: CC BY-SA 3.0
13 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 19, 2023 at 17:09 | comment | added | dumbass | −1 is the only index for which this works, though; it doesn’t generalize well to n-th from the end. | |
| Apr 10, 2018 at 10:57 | history | edited | DBS | CC BY-SA 3.0 | Fixed the miss-leading link that mentioned slice but linked to substring documentation |
| S Feb 1, 2018 at 12:41 | history | suggested | josemmo | CC BY-SA 3.0 | Fixed broken link |
| Feb 1, 2018 at 11:40 | review | Suggested edits | |||
| S Feb 1, 2018 at 12:41 | |||||
| Sep 3, 2016 at 16:13 | comment | added | Liggliluff | @stack The position (counting from 1) of the last character should be the length of a string, basically .length I'm not sure how it handles surrogate pairs. | |
| Sep 2, 2016 at 23:04 | comment | added | Yaron Levi | This code may be short and "sexy" but I won't use it. The code is not self explanatory and not clear. Use charAt instead. | |
| Jan 26, 2016 at 22:14 | comment | added | stack | Well, How can I get the position of lase character? "abc " Now I need to get 4 (there is a space in the end), How can I do that? | |
| Mar 4, 2014 at 20:00 | comment | added | gregtzar | @CMS You are right, your method does actually grab the last char of the string without trimming it. Nice elegant method too. For some reason I was thinking it was going to trim those chars and return a trimmed string. Sorry about that, I feel like an idiot for not trying it first. | |
| Mar 3, 2014 at 15:20 | comment | added | Christian C. Salvadó | @Egg, seems that you didn't try my example, str.slice(-1) does indeed get the last character of the string (as the OP required), just as if you used str.charAt(str.length - 1), try: "abc".slice(-1). I was showing here a usage of String.prototype.slice with a negative index. str.slice(-n) will get the last n characters of the original string as a new string value. More info: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… | |
| Dec 15, 2013 at 18:56 | comment | added | Ray Toal | What do you mean by a "UTF-8 string"? The phrase makes no sense. FWIW though, JavaScript strings are funny beasts themselves: they are sequences of unsigned 16-bit integer values. If the intent of your string is to store characters, then the code above returns the last 16-bit value, which would not be pretty if it was the second part of a surrogate pair. But again, what exactly do you mean by "UTF-8 string"? UTF-8 is an encoding scheme that might make sense if you had a byte array, but means nothing at all when it comes to strings. Just curious. | |
| Oct 7, 2010 at 19:08 | comment | added | Peter Ajtai | Side note: arrays also have a slice() method. - Their functionality is conceptually similar (partial copies) -------- (Just in case you're reading code and see .slice()) | |
| Oct 7, 2010 at 18:46 | history | edited | Christian C. Salvadó | CC BY-SA 2.5 | added 59 characters in body; deleted 41 characters in body |
| Oct 7, 2010 at 18:32 | history | answered | Christian C. Salvadó | CC BY-SA 2.5 |