Hướng dẫn substring last character javascript
How to get the last character of the string:
The last character of this string is How can I find this?
asked Oct 7, 2010 at 18:21
1 An elegant and short alternative, is the
Just by:
A negative start index slices the string from
DBS 8,3614 gold badges36 silver badges50 bronze badges answered Oct 7, 2010 at 18:32
7 Use
You can use this method in conjunction with the
dota2pro 6,6867 gold badges35 silver badges70 bronze badges answered Oct 7, 2010 at 18:23
DonutDonut 106k20 gold badges130 silver badges144 bronze badges 0 You can achieve this using different ways but with different performance, 1. Using bracket notation:
But it's not recommended to use brackets. Check the reasons here 2. charAt[index]:
This is readable and fastest among others. It is most recommended way. 3. substring:
4. slice:
It's slightly faster than substring. You can check the performance here With ES6: You can use But it is not supported in IE. Check more details about endsWith here answered Jul 6, 2017 at 13:34
VikasVikas 6,3512 gold badges24 silver badges38 bronze badges 3
Some browsers allow (as a non-standard extension) you to shorten this to:
answered Oct 7, 2010 at 18:22
Matthew FlaschenMatthew Flaschen 271k49 gold badges510 silver badges537 bronze badges 1 Use substr with parameter -1:
equals "." Note:
answered Apr 22, 2016 at 16:19
SpikolynnSpikolynn 3,9002 gold badges35 silver badges44 bronze badges 2 An easy way of doing it is using this :)
answered Jul 22, 2018 at 18:07
ConnieConnie 3553 silver badges12 bronze badges 4 Using the
Read more about
answered Feb 24 at 18:16
Ran TurnerRan Turner 10.8k3 gold badges31 silver badges41 bronze badges Try this...
adir abargil 4,9423 gold badges17 silver badges24 bronze badges answered Apr 9, 2018 at 23:14
Vishal KumarVishal Kumar 1,08311 silver badges13 bronze badges You can get the last char like this :
answered Oct 7, 2010 at 18:23
Colin HebertColin Hebert 89.6k15 gold badges156 silver badges148 bronze badges
answered Nov 22, 2016 at 8:25
SalarSalar 5,4195 gold badges43 silver badges70 bronze badges 1 Use the JavaScript charAt function to get a character at a given 0-indexed position. Use length to find out how long the String is. You want the last character so that's length - 1. Example:
answered Oct 7, 2010 at 18:24
AdamAdam 42.5k16 gold badges104 silver badges143 bronze badges If you have or are already using lodash, use last instead:
Not only is it more concise and obvious than the vanilla JS, it also safer since it avoids
answered Feb 16, 2021 at 5:42
xlmxlm 5,84913 gold badges51 silver badges52 bronze badges 1 You can use the following. In this case of last character it's an overkill but for a substring, its useful:
answered Jul 11, 2018 at 1:46
GauravsaGauravsa 6,0862 gold badges18 silver badges25 bronze badges
answered Jan 4 at 9:07
2 You can use this simple ES6 method
This will work in every browsers. answered Nov 13, 2018 at 13:17
vdegennevdegenne 10.8k14 gold badges75 silver badges101 bronze badges 3 |