es6 includes的用法----判断一个字符串或数组是否包含一个指定的值

句法:

str.includes(searchString , [position]) 

searchString在此字符串内搜索的字符串。

position 可选的字符串中开始搜索的位置searchString。(默认为0)。

返回值:

true:如果搜索字符串在给定字符串内的任何地方找到;返回true 

false:如果没有找到返回false

描述:

此方法可让您确定一个字符串是否包含另一个字符串。

includes()方法区分大小写。例如,以下表达式返回false:

'Blue Whale'.includes('blue'); // returns false

运用:

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

console.log(str.includes('To be'));       // true
console.log(str.includes('question'));    // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1));    // false
console.log(str.includes('TO BE'));       // false

填充工具:

此方法已添加到ECMAScript 2015规范中,可能尚未在所有JavaScript实现中提供。但是,您可以轻松地填充此方法:

if (!String.prototype.includes) {
  String.prototype.includes = function(search, start) {
    'use strict';
    if (typeof start !== 'number') {
      start = 0;
    }
    
    if (start + search.length > this.length) {
      return false;
    } else {
      return this.indexOf(search, start) !== -1;
    }
  };
}

不过填充工具是什么。最后一点没有看懂

 

补充es5

es5中是用indexOf的命令来查找的,存在的返回的是索引值,不存在返回-1,但是NaN查找不出来,因为NaN!==NaN

 

转载于:https://www.cnblogs.com/haonanya/p/9004419.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值