zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    网络流入门
    Sereja and Swaps(贪心+暴力枚举区间)
    multiset
    欧拉路
    整除分块
    蓝魔法师
    选点
    F. Tree with Maximum Cost(换根)
    “db2执行sql语句,注释没了”的解决办法
    Mybatis中的自带Mapper方法
  • 原文地址:https://www.cnblogs.com/haonanya/p/9004419.html
Copyright © 2011-2022 走看看