zoukankan      html  css  js  c++  java
  • JavaScript indexOf 理解

    indexof 对于匹配元素进行绝对定位 标准格式:

    stringObject.indexOf(searchvalue,fromindex)

     应用场景

    1.在string中查找

    var a = "abcdefg hello world!"

    console.log(a.indexOf("hello"));//8 stringObject 中的字符位置是从 0 开始的

    console.log(a.indexOf("Hello"));//-1 未匹配到字符串会返回-1   对大小写敏感

    console.log(a.indexOf("e"));//4 没有指定的情况下会返回第一次匹配到的位置

    console.log(a.indexOf("e",2));//9 在该字符串中查找第二次匹配到元素的绝对位置 “ ”空格占一个字符

    2.在数组中查找

    var str = ["Banana", "Orange", "Apple", "Mango","hello"];

    console.log(str.indexOf("hello"));//4 数组中会以 "," 为分隔符进行匹配元素定位

    console.log(str.indexOf("e"));//-1 以字符串进行匹配,单一元素会显示匹配失败

    拓展

    idexof方法多用于字符串匹配字符定位 匹配条件 不可以 正则表达式 

  • 相关阅读:
    Path Sum 解答
    Binary Tree Paths 解答
    Populating Next Right Pointers in Each Node 解答
    Validate Binary Search Tree 解答
    First Missing Positive
    78 Subsets
    47.Permutations 1&2
    N-Queens 1&2
    Combination Sum_DFS
    38.Count and Say
  • 原文地址:https://www.cnblogs.com/hanlengyao/p/10033412.html
Copyright © 2011-2022 走看看