[英]How to get the last character of a string?
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.一个优雅而简短的替代方法是
String.prototype.slice
方法。
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:负起始索引将字符串从
length+index
index 切片到length
,即索引-1
,提取最后一个字符:
"abc".slice(-1); // "c";
The charAt() method returns the character at the specified index in a string.
charAt() 方法返回字符串中指定索引处的字符。
You can use this method in conjunction with the length
property of a string to get the last character in that string.您可以将此方法与字符串的
length
属性结合使用以获取该字符串中的最后一个字符。
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
str.charAt(str.length - 1)
某些浏览器允许(作为非标准扩展)您将其缩短为:
str[str.length - 1];
You can achieve this using different ways but with different performance,您可以使用不同的方式但具有不同的性能来实现这一点,
1. Using bracket notation: 1. 使用括号表示法:
var str = "Test"; var lastLetter = str[str.length - 1];
But it's not recommended to use brackets.但不建议使用括号。 Check the reasons here
检查这里的原因
2. charAt[index]: 2.charAt[索引]:
var lastLetter = str.charAt(str.length - 1)
This is readable and fastest among others.这是可读和最快的。 It is most recommended way.
这是最推荐的方式。
3. substring: 3.子串:
str.substring(str.length - 1);
4. slice: 4.切片:
str.slice(-1);
It's slightly faster than substring.它比子字符串略快。
You can check the performance here您可以在此处查看性能
With ES6:使用 ES6:
You can use str.endsWith("t");
您可以使用
str.endsWith("t");
But it is not supported in IE.但它在 IE 中不受支持。 Check more details about endsWith here
在此处查看有关endsWith的更多详细信息
An easy way of doing it is using this :)一个简单的方法是使用这个:)
var word = "waffle"
word.endsWith("e")
您可以像这样获得最后一个字符:
var lastChar=yourString.charAt(yourString.length-1);
Try this...试试这个...
const str = "linto.yahoo.com."
console.log(str.charAt(str.length-1));
Use the JavaScript charAt function to get a character at a given 0-indexed position.使用 JavaScript charAt函数在给定的 0 索引位置获取字符。 Use length to find out how long the String is.
使用length找出字符串的长度。 You want the last character so that's length - 1. Example:
您需要最后一个字符,因此长度为 - 1。示例:
var word = "linto.yahoo.com.";
var last = word.charAt(word.length - 1);
alert('The last character is:' + last);
var firstName = "Ada";
var lastLetterOfFirstName = firstName[firstName.length - 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");
If you have or are already using lodash, use last instead:如果您已经或正在使用 lodash,请改用last :
_.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:它不仅比 vanilla JS 更简洁明了,而且更安全,因为它避免了
Uncaught TypeError: Cannot read property X of undefined
when the input is null
or undefined
所以你不需要事先检查这个:
// 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);
How to get the last character of the string:如何获取字符串的最后一个字符:
"linto.yahoo.com."
The last character of this string is "."
该字符串的最后一个字符是
"."
How can I find this?我怎么能找到这个?
You can use this simple ES6 method你可以使用这个简单的 ES6 方法
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.这将适用于所有浏览器。
var string = "Hello"; var fg = string.length; fg = fg - 1; alert(string[fg]);
You can also convert string to array and get the last item, 您还可以将字符串转换为数组并获取最后一项,
var str = "Hello world!";
var arr = str.split('');
var lastItem = arr[arr.length - 1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.