在JavaScript中,字符串类型String和数组类型Array都有indexOf()方法,虽然他们的作用都是返回传入元素在指定字符串或数组中的位置,但他们之间还是存在着一点点不同。

Str.indexOf(searchValue[, fromIndex])

字符串调用indexOf方法,返回searchValue在原字符串中第一次出现的位置,可以使用lastIndexOf方法查找searchValue最后一次出现的位置,如果没有找到则返回-1。

它与数组的indexOf方法不同的是,它比较元素时会存在类型转换。

console.log('12345'.indexOf(3)); //
console.log('12345'.indexOf([3])); //
console.log('12345'.indexOf(new Object(3))); //

这里传入的实际上分别是数值类型、数组类型、对象,但是它还是返回了这个元素在字符串中的位置。

console.log('12345'.indexOf(45)); //
console.log('12345'.indexOf([45])); //
console.log('12345'.indexOf(new Object(45))); //

从以上例子可以看到实际上这个函数把传入的元素转换为了字符串,然后按照匹配子字符串的方式找到它在原字符串中的位置。

另外indexOf方法对大小写敏感。

str.search(RegExp)

顺便再提一下search方法,search方法用于检索字符串中指定的子字符串,检索与正则表达式相匹配的子字符串。如果没有找到,返回-1。

search() 方法不执行全局匹配,它将忽略标志 g。它同时忽略 regexp 的 lastIndex 属性,并且总是从字符串的开始进行检索,这意味着它总是返回 str 的第一个匹配的位置。它同样对大小写敏感,如果要忽略大小写则在RegExp中添加标志i。

Arr.indexOf(searchValue[, fromIndex])

同样返回的是searchValue在Arr中第一次出现的位置,但是数组的indexOf方法执行的是严格相等,也就是“===”,不会有类型转换。

let arr=['apple','banana','12','apple'];
console.log(arr.indexOf('apple')); //
console.log(arr.indexOf('apple',1)); //
console.log(arr.indexOf(12)); //-1
console.log(arr.indexOf('app')); //-1

【20190407】JavaScript-indexOf方法解析的更多相关文章

  1. JavaScript indexOf() 方法

    定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法 stringObject.indexOf(searchvalue,fromindex) 说明 该方法将从头 ...

  2. JavaScript indexOf() 方法 和 lastIndexOf() 方法

    indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索. 语法: in ...

  3. JavaScript indexOf() 方法详解

    定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法 stringObject.indexOf(searchvalue,fromindex) 参数 描述 sea ...

  4. JavaScript indexof方法

    1.indexof方法 indexof方法可以在字符串和数组上使用. 2.字符串使用 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. <!DOCTYPE html&g ...

  5. JavaScript indexOf() 方法,获取元素的位置;Object.keys()获取对象的所有key的数组

    定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法 stringObject.indexOf(searchvalue,fromindex) 参数 描述 sea ...

  6. javascript 数组方法解析

    测试数组:testArrayA = ['a','b','c','d','e'] , testArrayB = [2,3,6,1] 1.删除数组最后一项(pop()): 返回删除那一项的值:var po ...

  7. JavaScript—从数组的indexOf方法深入——Object的Property机制。

    在js中,可以说万物皆对象(object),一个数组也是一个对象(array). 很多对象都有很多很方便的方法 比如数组的push,concat,slice等等,但是如果一些对象,它没有实现这些方法, ...

  8. js 判断数组包含某值的方法 和 javascript数组扩展indexOf()方法

    var  questionId = []; var anSwerIdValue = []; ////javascript数组扩展indexOf()方法 Array.prototype.indexOf ...

  9. JavaScript indexOf() 方法和 lastIndexOf() 方法

    一,定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索 ...

随机推荐

  1. Java作业九(2017-11-6)

    /*圆的类*/ public class R { private double radius; // 构造方法,有参构造 public R(double radius) { this.radius = ...

  2. MySQL乐观锁为什么可以防止并发

    问题引入 本文介绍的是最常用的也是mysql默认的innoDB引擎 Read committed隔离级别下事物的并发.这种情况下的事物特点是 读:在一个事物里面的select语句 不会受到其他事物(不 ...

  3. [Swift]LeetCode31. 下一个排列 | Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  4. [Swift]LeetCode658. 找到 K 个最接近的元素 | Find K Closest Elements

    Given a sorted array, two integers k and x, find the kclosest elements to x in the array. The result ...

  5. [Swift]LeetCode838. 推多米诺 | Push Dominoes

    There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...

  6. AI - TensorFlow - 起步(Start)

    01 - 基本的神经网络结构 输入端--->神经网络(黑盒)--->输出端 输入层:负责接收信息 隐藏层:对输入信息的加工处理 输出层:计算机对这个输入信息的认知 每一层点开都有它相应的内 ...

  7. java 随机数产生 常用类及方法

    1.Random类 Random():创建一个新的随机数生成器. new一个Random类的对象: Random r = new Random(); 利用该对象产生一个随机整数:常用nextInt,不 ...

  8. ThinkPHP 数据库操作(五) : 存储过程、数据集、分布式数据库

    存储过程 5.0支持存储过程,如果我们定义了一个数据库存储过程 sp_query ,可以使用下面的方式调用: $result = Db::query('call sp_query(8)'); 返回的是 ...

  9. 说一说MVC的Authentication过滤(四)

    前沿: 一般情况下,在我们做访问权限管理的时候,会把用户的正确登录后的基本信息保存在Session中,以后用户每次请求页面或接口数据的时候,拿到 Session中存储的用户基本信息,查看比较他有没有登 ...

  10. 说一说MVC的控制器(二)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...