search,index, charAt, find,findIndex , includes 区别

  • search, indexOf : 根据str,找下标,返回第一个出现。 只 字符串可用,数字类型(number)不可用。-----大小写敏感
  • charAt: 根据下标,找对应字符,返回第一个出现。 字符串,数组可用,数字类型(number)不可用。 -----大小写敏感

(ES6为Array增加了find(),findIndex函数。)

  • find():返回符合查找条件的目标元素,找不到返回undefined。只数组可用
  • findIndex():返回第一个符合查找条件的目标元素的位置,找不到就返回-1。只数组可用。-----大小写敏感

(find、findIndex 都是一个查找回调函数。)

  • includes:(es6)  判断一个数组/字符串是否包含一个指定的值,如果是返回 true,否则false。 字符串,数组可用,数字类型(number)不可用。 -----大小写敏感
  • search:匹配第一个出现的符合条件str下标,从0开始,没有返回-1。识别大小写(大小写敏感)

  • 字符串方法,数组不可用
var str="Visit Runoob!"; 
var n=str.search("Runoob");      //    6

//执行一次对大小写敏感的查找:
var str="Mr. Blue has a blue house"
var n=str.search("blue");      //    15
var n=str.search("b");         //    15
var n=str.search("Blue");      //    4
var n=str.search("B");         //    4

//执行一次忽略大小写的检索 ( 正 则 匹配 )
var str="Mr. Blue has a blue house";
str.search(/blue/i)   //    4、

'2524231'.search(3)   //  5
'2524231'.search('3')   //  5
//--------------------------------------分割线----------------------------------

// 数字不可用   indexOf --- 亦数字不可用,报错
var str=(7638244234);
var n=str.search(3);  //  str.search is not a function 报错


// 数组不可用
[1,2,3,4,5,6,8,5,4,4,4,3].search(3) //str.search is not a function 报错
['1','2','3','4','3'].search(3) //str.search is not a function 报错
  • indexOf():匹配第一个。对大小写敏感。从0开始,没有返回-1。识别大小写,大小写敏感

  • 数组、字符串 ,均可用。数组调用时,有类型识别(相当于===判断)
  • 参数2:  indexOf(str,startIndex)
var str="Hello world!"
str.indexOf("Hello")  // 0
str.indexOf("World")  // -1
str.indexOf("world")  // 6

//第二个参数用法
var str1="Hello world, welcome to the universe.";
var n=str.indexOf("e",5);//从下标5开始,查找e第一次出现的下标位置
// 14

//数组调用indexOf   --- 识别类型
[1,2,3,4,5,4,3].indexOf('3',-58)   //-1
[1,2,3,4,5,4,3].indexOf(3,-58)   // 2

 

  • charAt(): 返回指定位置的字符。从0开始

var str = "HELLO WORLD";
var n = str.charAt(2)    //  L

indexOf、search区别:(及何时用)

  1. indexOf()是比search()更加底层的方法。如果只是兑一个具体字符串来茶渣检索,那么使用indexOf()的系统资源消耗更小,效率更高。
  2. 如果查找具有某些特征的字符串(例如查找以a开头,后面是数字的字符串),那么indexOf()就无能为力,必须要使用正则表达式和search()方法了。
  3. 大多是时候用indexOf()不是为了真的想知道子字符串的位置,而是想知道长字符串中有没有包含这个子字符串。若果返回索引为-1,那么说明没有,反之则有。

--------------------------------------------------------------------------分割线--------------------------------------------------------------------------

  • find() 返回符合条件的数组的第一个元素的值。

  • 为数组中的每个元素都调用一次函数执行,当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。如果没有符合条件的元素返回 undefined

//find方法
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].find((value, index, arr) => {
  return value > 4
})
//  5


 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].find((value, index, arr) => {
  return value > 14
})
//    undefined 


['bot;', 'tom', 'tom'].find((value, index, arr) => {
    return age == 'tom'
})
// tom   此用法无意义
  • findIndex():数组方法,字符串不可用

findIndex() :传入一个测试条件(函数),返回符合条件的数组第一个元素位置。

(findIndex() :为数组中的每个元素都调用一次函数执行:

  • 当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。
  • 如果没有符合条件的元素返回 -1)
// findIndex 方法
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].findIndex((value, index, arr) => {
  return value > 4
})
//   4

 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].findIndex((value, index, arr) => {
  return value > 14
})
//   -1

['bot;', 'tom', 'tom'].findIndex((value, index, arr) => {
    return age == 'tom'
})
// 1

//字符串不可使用findIndex及find方法
'kdsfjdsdttf'.findIndex((val, index, arr) => {
    return val == 't'
})
//Uncaught TypeError: ages.findIndex is not a function  报错
// find 、 findindex---查找NAN
 [1, 2, NaN, 4, 5, 6, 7, 8, 9, 10, 11].find((value, index, arr) => {
  return Object.is(NaN, value)
})
//  NaN 

 [1, 2, NaN, 4, 5, 6, 7, 8, 9, 10, 11].findIndex((value, index, arr) => {
  return Object.is(NaN, value)
})
//  2 
  • includes():数组,字符串方法。识别大小写,大小写敏感

  • 数组、字符串 ,均可用。数组调用时,Array类型识别(相当于===判断),str(number) 无类型识别 '1',1 都会被查出来 
  • 参数2: arr.includes(arr/str, startIndex) :可选。从该索引处开始查找 searchElement。如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索。默认为 0。
let site = ['runoob', 'google', 'taobao'];
	
site.includes('runoob')     //true
site.includes('runooB')     //false
site.includes('baidu')      //false

let str = 'runoob,google,taobao';

str.includes('runo')     //true
str.includes('runoob')     //true
str.includes('runooB')     //false
str.includes('baidu')      //false

let arr_num = ['11','2','3'];

//注意:有类型区别,11 :false , '11': true

arr_num.includes('11')     //true

arr_num.includes(11)     //false

arr_num.includes(12)      //false

let str_num = '11,2,3';

//注意:11也包含1,所以也为true

str_num.includes('1')     //true
str_num.includes(1)     //true
str_num.includes(12)      //false
str_num.includes('12')      //false

let num = 123;

num.includes(1)    //num.includes is not a function 报错

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值