endsWith在JavaScript中 [英] endsWith in JavaScript

查看:127
本文介绍了endsWith在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?


  1. 是否有 endsWith() JavaScript中的方法?

  1. Is there a endsWith() method in JavaScript?

我所拥有的一个解决方案是获取字符串的长度并获取最后一个字符并进行检查。

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

TJ Crowder - 在现代浏览器上创建子串并不昂贵;它可能是在2010年这个答案发布时。这些天,简单的 this.substr(-suffix.length)===后缀方法在Chrome上最快,在IE11上与indexOf相同,只有4%慢(在Firefox上: jsperf.com/endswith-stackoverflow/14 全面加快结果是错误的: jsperf.com/endswith-stackoverflow-when-false 当然,随着ES6添加endsWith,这一点没有实际意义。 : - )

T.J. Crowder - 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. :-)

原始答案:

我知道这是一年前的问题...但我也需要这个,我需要它来跨浏览器工作...... strong>结合每个人的答案和评论并简化它:

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 <的第二个参数跳过不必要的比较/ code>向前跳过

  • 在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在评论中指出的那样,如果您想在安全方面犯错并检查是否已提供实施,您只需添加类型检查如下:


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

      这篇关于endsWith在JavaScript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆