How to get the last character of the string:
"linto.yahoo.com."
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
String.prototype.slice
method.
Just by:
str.slice[-1];
A negative start index slices the string from length+index
, to length
, being index -1
, the last character is extracted:
"abc".slice[-1]; // "c";
DBS
8,3614 gold badges36 silver badges50 bronze badges
answered Oct 7, 2010 at 18:32
7
Use charAt
:
The charAt[] method returns the character at the specified index in a string.
You can use this method in conjunction with the
length
property of a string to get the last character in that string.
For example:
const myString = "linto.yahoo.com.";
const stringLength = myString.length; // this will be 16
console.log['lastChar: ', myString.charAt[stringLength - 1]]; // this will be the string
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:
var str = "Test";
var lastLetter = str[str.length - 1];
But it's not recommended to use brackets. Check the reasons here
2. charAt[index]:
var lastLetter = str.charAt[str.length - 1]
This is readable and fastest among others. It is most recommended way.
3. substring:
str.substring[str.length - 1];
4. slice:
str.slice[-1];
It's slightly faster than substring.
You can check the performance here
With ES6:
You can use str.endsWith["t"];
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
str.charAt[str.length - 1]
Some browsers allow [as a non-standard extension] you to shorten this to:
str[str.length - 1];
answered Oct 7, 2010 at 18:22
Matthew FlaschenMatthew Flaschen
271k49 gold badges510 silver badges537 bronze badges
1
Use substr with parameter -1:
"linto.yahoo.com.".substr[-1];
equals "."
Note:
To extract characters from the end of the string, use a negative start number [This does not work in IE 8 and earlier].
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 :]
var word = "waffle"
word.endsWith["e"]
answered Jul 22, 2018 at 18:07
ConnieConnie
3553 silver badges12 bronze badges
4
Using the String.prototype.at[]
method is a new way to achieve it
const s = "linto.yahoo.com.";
const last = s.at[-1];
console.log[last];
Read more about at
here
answered Feb 24 at 18:16
Ran TurnerRan Turner
10.8k3 gold badges31 silver badges41 bronze badges
Try this...
const str = "linto.yahoo.com."
console.log[str.charAt[str.length-1]];
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 :
var lastChar=yourString.charAt[yourString.length-1];
answered Oct 7, 2010 at 18:23
Colin HebertColin Hebert
89.6k15 gold badges156 silver badges148 bronze badges
var firstName = "Ada";
var lastLetterOfFirstName = firstName[firstName.length - 1];
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:
var word = "linto.yahoo.com.";
var last = word.charAt[word.length - 1];
alert['The last character is:' + last];
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:
_.last[str];
Not only is it more concise and obvious than the vanilla JS, it also safer since it avoids Uncaught TypeError: Cannot read property X of undefined
when the input is null
or undefined
so you don't need to check this beforehand:
// Will throws Uncaught TypeError if str is null or undefined
str.slice[-1]; //
str.charAt[str.length -1];
// Returns undefined when str is null or undefined
_.last[str];
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:
var word = "linto.yahoo.com.";
var last = ".com.";
if [word.substr[-[last.length]] == last]
alert["its a match"];
answered Jul 11, 2018 at 1:46
GauravsaGauravsa
6,0862 gold badges18 silver badges25 bronze badges
var string = "Hello";
var fg = string.length;
fg = fg - 1;
alert[string[fg]];
answered Jan 4 at 9:07
2
You can use this simple ES6 method
const lastChar = [str] => str.split[''].reverse[].join[','].replace[',', ''][str.length === str.length + 1 ? 1 : 0];
// example
console.log[lastChar["linto.yahoo.com."]];
This will work in every browsers.
answered Nov 13, 2018 at 13:17
vdegennevdegenne
10.8k14 gold badges75 silver badges101 bronze badges
3