如何检查字符串是否以JavaScript中的特定字符结束?

示例:我有一个字符串

var str = "mystring#";

我想知道字符串是否以#结尾。我怎么检查呢?

在JavaScript中有endsWith()方法吗? 我有一个解决方案,取字符串的长度,并得到最后一个字符,并检查它。

这是最好的办法还是还有别的办法?


当前回答

更新(2015年11月24日):

这个答案最初发布于2010年(六年前),所以请注意这些有见地的评论:

绍纳-

google用户更新-看起来ECMA6增加了这个功能。MDN文章还展示了一个polyfill。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

T.J.克劳德-

在现代浏览器中创建子字符串并不昂贵;这个答案很可能是在2010年发布的。现在,简单的this.substr(-suffix.length) ===后缀方法在Chrome上是最快的,在IE11上和indexOf一样,在Firefox上只慢了4% (fergetaboutit领域):https://jsben.ch/OJzlM并且在结果为false时更快:jsperf.com/endswith-stackoverflow-when-false当然,随着ES6添加endsWith,这一点是没有意义的。: -)


最初的回答:

我知道这是一个老问题了……但我也需要这个,我需要它跨浏览器工作,所以……结合每个人的回答和评论,并简化一下:

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

不创建子字符串 使用本机indexOf函数以获得最快的结果 使用indexOf的第二个参数跳过不必要的比较 在Internet Explorer中工作 没有正则表达式的复杂性


此外,如果你不喜欢在原生数据结构的原型中填充东西,这里有一个独立的版本:

function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

编辑:正如@hamish在评论中指出的那样,如果你想在安全方面犯错,检查是否已经提供了一个实现,你可以像这样添加一个typeof检查:

if (typeof String.prototype.endsWith !== 'function') {
    String.prototype.endsWith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };
}

其他回答

String.prototype.endsWith()

总结

endsWith()方法确定字符串是否以另一个字符串的字符结束,并根据需要返回true或false。

语法

str.endsWith(searchString [, position]);

参数

searchString: 在此字符串的末尾要搜索的字符。 位置: 在这个字符串中搜索,就好像这个字符串只有这么长;默认为此字符串的实际长度,在此字符串长度建立的范围内。

描述

此方法允许您确定字符串是否以另一个字符串结束。

例子

var str = "To be, or not to be, that is the question.";

alert( str.endsWith("question.") );  // true
alert( str.endsWith("to be") );      // false
alert( str.endsWith("to be", 19) );  // true

规范

ECMAScript语言规范第六版(ECMA-262)

浏览器兼容性

String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}

String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}

我希望这对你们有帮助

var myStr = “  Earth is a beautiful planet  ”;
var myStr2 = myStr.trim();  
//==“Earth is a beautiful planet”;

if (myStr2.startsWith(“Earth”)) // returns TRUE

if (myStr2.endsWith(“planet”)) // returns TRUE

if (myStr.startsWith(“Earth”)) 
// returns FALSE due to the leading spaces…

if (myStr.endsWith(“planet”)) 
// returns FALSE due to trailing spaces…

传统的方式

function strStartsWith(str, prefix) {
    return str.indexOf(prefix) === 0;
}

function strEndsWith(str, suffix) {
    return str.match(suffix+"$")==suffix;
}

@chakrit的公认答案是一个坚实的方法来做自己。但是,如果您正在寻找一个打包的解决方案,我建议您考虑一下underscore。字符串,正如@mlunoe指出的那样。使用下划线。字符串,代码将是:

function endsWithHash(str) {
  return _.str.endsWith(str, '#');
}

更新(2015年11月24日):

这个答案最初发布于2010年(六年前),所以请注意这些有见地的评论:

绍纳-

google用户更新-看起来ECMA6增加了这个功能。MDN文章还展示了一个polyfill。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

T.J.克劳德-

在现代浏览器中创建子字符串并不昂贵;这个答案很可能是在2010年发布的。现在,简单的this.substr(-suffix.length) ===后缀方法在Chrome上是最快的,在IE11上和indexOf一样,在Firefox上只慢了4% (fergetaboutit领域):https://jsben.ch/OJzlM并且在结果为false时更快:jsperf.com/endswith-stackoverflow-when-false当然,随着ES6添加endsWith,这一点是没有意义的。: -)


最初的回答:

我知道这是一个老问题了……但我也需要这个,我需要它跨浏览器工作,所以……结合每个人的回答和评论,并简化一下:

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

不创建子字符串 使用本机indexOf函数以获得最快的结果 使用indexOf的第二个参数跳过不必要的比较 在Internet Explorer中工作 没有正则表达式的复杂性


此外,如果你不喜欢在原生数据结构的原型中填充东西,这里有一个独立的版本:

function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

编辑:正如@hamish在评论中指出的那样,如果你想在安全方面犯错,检查是否已经提供了一个实现,你可以像这样添加一个typeof检查:

if (typeof String.prototype.endsWith !== 'function') {
    String.prototype.endsWith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };
}
return this.lastIndexOf(str) + str.length == this.length;

在原始字符串长度小于搜索字符串长度并且没有找到搜索字符串的情况下不工作:

lastIndexOf返回-1,然后添加搜索字符串的长度,剩下的是原始字符串的长度。

一个可能的解决方案是

return this.length >= str.length && this.lastIndexOf(str) + str.length == this.length