简体   繁体   English

如何获取字符串的最后一个字符?

[英]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";

Use charAt :使用charAt

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的更多详细信息

Use substr with parameter -1 :substr与参数-1一起使用:

"linto.yahoo.com.".substr(-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).要从字符串的末尾提取字符,请使用负的起始编号(这在 IE 8 及更早版本中不起作用)。

Using the String.prototype.at() method is a new way to achieve it使用String.prototype.at()方法是一种新的实现方式

 const s = "linto.yahoo.com."; const last = s.at(-1); console.log(last);

Read more about at here at 此处阅读更多信息

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM