在 JavaScript 中以结尾 [英] endsWith in JavaScript
问题描述
如何在 JavaScript 中检查字符串是否以特定字符结尾?
How can I check if a string ends with a particular character in JavaScript?
示例:我有一个字符串
var str = "mystring#";
我想知道该字符串是否以 #
结尾.如何检查?
I want to know if that string is ending with #
. How can I check it?
JavaScript 中有
endsWith()
方法吗?
我的一个解决方案是获取字符串的长度并获取最后一个字符并检查它.
One solution I have is take the length of the string and get the last character and check it.
这是最好的方法还是有其他方法?
Is this the best way or there is any other way?
推荐答案
更新(2015 年 11 月 24 日):
这个答案最初是在 2010 年(六年前)发布的.所以请注意这些有见地的评论:
This answer is originally posted in the year 2010 (SIX years back.) so please take note of these insightful comments:
- Shauna -
针对 Google 员工的更新 - 看起来 ECMA6 添加了此功能.MDN 文章还展示了一个 polyfill.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
Update for Googlers - Looks like ECMA6 adds this function. The MDN article also shows a polyfill. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
- T.J.克劳德 -
在现代浏览器上创建子字符串并不昂贵;这个答案很可能是在 2010 年发布的.现在,简单的 this.substr(-suffix.length) === suffix
方法在 Chrome 上最快,在 IE11 上与 indexOf 相同,在 Firefox 上仅慢 4%(fergetaboutit 区域):jsperf.com/endswith-stackoverflow/14 当结果为假时,速度更快:jsperf.com/endswith-stackoverflow-when-false 当然,用ES6 添加了endsWith,这点没有实际意义.:-)
Creating substrings isn't expensive on modern browsers; it may well have been in 2010 when this answer was posted. These days, the simple
this.substr(-suffix.length) === suffix
approach is fastest on Chrome, the same on IE11 as indexOf, and only 4% slower (fergetaboutit territory) on Firefox: jsperf.com/endswith-stackoverflow/14 And faster across the board when the result is false: jsperf.com/endswith-stackoverflow-when-false Of course, with ES6 adding endsWith, the point is moot. :-)
原始答案:
我知道这是一个老问题......但我也需要这个并且我需要它跨浏览器工作所以......结合每个人的答案和评论并稍微简化一下:
I know this is a year old question... but I need this too and I need it to work cross-browser so... combining everyone's answer and comments and simplifying it a bit:
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
- 不创建子字符串
- 使用原生
indexOf
函数以获得最快的结果 - 使用
indexOf
的第二个参数跳过不必要的比较以向前跳过 - 可在 Internet Explorer 中使用
- 没有正则表达式的并发症
- Doesn't create a substring
- Uses native
indexOf
function for fastest results - Skip unnecessary comparisons using the second parameter of
indexOf
to skip ahead - Works in Internet Explorer
- NO Regex complications
此外,如果您不喜欢在本机数据结构的原型中填充东西,这里有一个独立版本:
Also, if you don't like stuffing things in native data structure's prototypes, here's a standalone version:
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
正如@hamish 在评论中所指出的,如果您想在安全方面犯错并检查是否已经提供了一个实现,您只需添加一个 typeof
像这样检查:
As noted by @hamish in the comments, if you want to err on the safe side and check if an implementation has already been provided, you can just adds a typeof
check like so:
if (typeof String.prototype.endsWith !== 'function') {
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
}
这篇关于在 JavaScript 中以结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!