How to get the last character of the string:
"linto.yahoo.com." The last character of this string is "."
How can I find this?
How to get the last character of the string:
"linto.yahoo.com." The last character of this string is "."
How can I find this?
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"; slice() method. - Their functionality is conceptually similar (partial copies) -------- (Just in case you're reading code and see .slice())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/…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 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
str.slice(-1);endsWith fits here, that does something completely different.put chars with BN shouldn't be coding.str.charAt(str.length - 1) Some browsers allow (as a non-standard extension) you to shorten this to:
str[str.length - 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).
Using the String.prototype.at() method is a new way to achieve this
const s = "linto.yahoo.com."; const last = s.at(-1); console.log(last); Read more about at
slice() vs at(); What's more readable: "string".at(pos) vs "string".slice(pos) There's also "string".charAt() and I would say that is better than slice() but less desirable than at()var firstName = "Ada"; var lastLetterOfFirstName = firstName[firstName.length - 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); 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 throw 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); try...catchYou can use substring() method :
let string = "linto.yahoo.com."
console.log(string.substring(string.length-1))
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.
(str) => str.split('').reverse()[0]? :)const lastChar=str.split('').pop()