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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3884632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:30:08  来源:igfitidea点击:

how to get the last character of a string?

javascript

提问by Linto P D

How to get the last character of the string:

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

"linto.yahoo.com."

The last character of this string is "."

这个字符串的最后一个字符是 "."

How can I find this?

我怎样才能找到这个?

回答by CMS

An elegant and short alternative, is the String.prototype.slicemethod.

一个优雅而简短的替代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, 到切片length,作为 index -1,提取最后一个字符:

"abc".slice(-1); // "c";

回答by Donut

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 lengthproperty of a string to get the last character in that string.
For example:

您可以将此方法与length字符串的属性结合使用以获取该字符串中的最后一个字符。
例如:

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

回答by Matthew Flaschen

str.charAt(str.length - 1)

Some browsers allow (as a non-standard extension) you to shorten this to:

某些浏览器允许(作为非标准扩展)您将其缩短为:

str[str.length - 1];

回答by Code_Mode

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];

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)

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);

str.substring(str.length - 1);

4. slice:

4.切片:

str.slice(-1);

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. Check more details about endsWith here

但它在 IE 中不受支持。在此处查看有关endsWith的更多详细信息

回答by Spikolynn

Use substrwith 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 及更早版本中不起作用)。

回答by Connie

An easy way of doing it is using this :)

一个简单的方法是使用这个:)

var word = "waffle"
word.endsWith("e")

回答by Colin Hebert

You can get the last char like this :

您可以像这样获得最后一个字符:

var lastChar=yourString.charAt(yourString.length-1);

回答by Adam

Use the JavaScript charAtfunction to get a character at a given 0-indexed position. Use lengthto find out how long the String is. You want the last character so that's length - 1. Example:

使用 JavaScript charAt函数在给定的 0 索引位置获取字符。使用length找出字符串的长度。您需要最后一个字符,因此长度为 - 1。示例:

var word = "linto.yahoo.com.";
var last = word.charAt(word.length - 1);
alert('The last character is:' + last);

回答by Salar

var firstName = "Ada";
var lastLetterOfFirstName = firstName[firstName.length - 1];

回答by vdegenne

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.

这将适用于所有浏览器。