zoukankan      html  css  js  c++  java
  • JS字符串常用方法(自)---8、字符串查找

    JS字符串常用方法(自)---8、字符串查找

    一、总结

    一句话总结:

    字符串查找方法有6个,startsWith(searchString,position), endsWith(searchString,length), search(regexp), indexOf(searchValue,fromIndex), lastIndexOf(searchValue,fromIndex), includes(searchString,position)

    startsWith(searchString,position),endsWith(searchString,length)
    search(regexp)
    indexOf(searchValue,fromIndex),lastIndexOf(searchValue,fromIndex)
    includes(searchString,position)

    1、lastIndexOf()的第二个参数fromIndex注意点是什么?

    从fromIndex往左查,所以'hello world, hello world'.lastIndexOf('llo',3)的结果是2

    2、关于找索引值的函数的返回值的共性是什么?

    关于找索引值的函数,找到就是返回的找到的索引,没找到就返回-1

    3、startsWith()方法和endsWith()方法的第二个参数的区别是什么?

    startsWith()方法的第二个参数表示要查找的位置,endsWith()方法的第二个参数表示指定字符串搜索的长度。

    二、字符串查找

    博客对应课程的视频位置:

    1、startsWith(searchString,position)

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>startsWith()</title>
     6 </head>
     7 <body>
     8 <!--
     9 startsWith(searchString,position)
    10 作用:判断当前字符串是否以另外一个给定的子字符串开头
    11 参数:必带的searchString(要搜索的子字符串),可选的position(搜索的位置)
    12 返回值:在字符串的开头找到了给定的字符则返回true;否则, 返回false.
    13 
    14 -->
    15 <script>
    16     var str = "To be, or not to be, that is the question.";
    17 
    18     console.log(str.startsWith("To be"));         // true
    19     console.log(str.startsWith("not to be"));     // false
    20     console.log(str.startsWith("not to be", 10)); // true
    21 </script>
    22 </body>
    23 </html>

    2、endsWith(searchString,length)

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>endsWith()</title>
     6 </head>
     7 <body>
     8 <!--
     9 
    10 endsWith(searchString,length)
    11 作用:判断当前字符串是否是以另外一个给定的子字符串“结尾”的
    12 参数:必带的searchString(要搜索的子字符串),可选的length(字符串的长度)
    13 返回值:true或者false
    14 
    15 startsWith()方法和endsWith()方法的第二个参数的区别是什么
    16 startsWith()方法的第二个参数表示要查找的位置,endsWith()方法的第二个参数表示指定字符串搜索的长度。
    17 
    18 
    19 
    20 
    21 
    22 
    23 -->
    24 <script>
    25     var str = "To be, or not to be, that is the question.";
    26 
    27     console.log( str.endsWith("question.") );  // true
    28     console.log( str.endsWith("to be") );      // false
    29     console.log( str.endsWith("to be", 19) );  // true
    30 </script>
    31 </body>
    32 </html>

    3、search(regexp)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>search()</title>
    </head>
    <body>
    <!--
    
    search(regexp)
    作用:利用正则表达式在字符串中查找
    参数:regexp(正则表达式)
    返回值:返回首次匹配项的索引
    
    
    -->
    <script>
        var str = "hey JudE";
        var re = /[A-Z]/g;
        var re2 = /[.]/g;
        console.log(str.search(re)); // returns 4, which is the index of the first capital letter "J"
        console.log(str.search(re2)); // returns -1 cannot find '.' dot punctuation
    </script>
    </body>
    </html>

    4、indexOf(searchValue,fromIndex)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>indexOf()</title>
    </head>
    <body>
    <!--
    indexOf(searchValue,fromIndex)
    作用:查找字符串第一次出现的位置
    参数:必带的searchValue(要被查找的字符串值),可选的fromIndex(查找的位置)
    返回值:找到的索引值,没找到返回-1
    
    关于找索引值的函数的返回值的共性是什么
    关于找索引值的函数,找到就是返回的找到的索引,没找到就返回-1
    
    -->
    <script>
        console.log('hello world'.indexOf('llo'));
        console.log('hello world'.indexOf('llo',3));
    </script>
    </body>
    </html>

    5、lastIndexOf(searchValue,fromIndex)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>lastIndexOf()</title>
    </head>
    <body>
    <!--
    lastIndexOf(searchValue,fromIndex)
    作用:查找字符串最后一次出现的位置
    参数:必带的searchValue(要被查找的字符串值),可选的fromIndex(查找的位置)
    返回值:找到的索引值,没找到返回-1
    
    lastIndexOf()的第二个参数fromIndex注意点是什么
    从fromIndex往左查,所以'hello world, hello world'.lastIndexOf('llo',3)的结果是2
    
    
    -->
    <script>
        console.log('hello world, hello world'.lastIndexOf('llo'));//15
        console.log('hello world, hello world'.lastIndexOf('llo',3));//2
    </script>
    </body>
    </html>

    6、includes(searchString,position)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>includes()</title>
    </head>
    <body>
    <!--
    
    includes(searchString,position)
    作用:判断一个字符串是否包含在另一个字符串中
    参数:必带的searchString,可选的position(搜索的位置)
    返回值:true或者false
    
    -->
    <script>
        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
    </script>
    </body>
    </html>
     
  • 相关阅读:
    【weka应用技术与实践】过滤器
    【软件分析与挖掘】Vision of Software Clone Management: Past, Present, and Future (Keynote Paper)
    【软件分析与挖掘】Multiple kernel ensemble learning for software defect prediction
    【Thinking in Java-CHAPTER 3】操作符
    【软件分析与挖掘】A Comparative Study of Supervised Learning Algorithms for Re-opened Bug Prediction
    【Thinking in Java-CHAPTER 1&&2】对象导论&&一切都是对象
    【cs229-Lecture20】策略搜索
    【cs229-Lecture19】微分动态规划
    【cs229-Lecture18】线性二次型调节控制
    【cs229-Lecture17】离散与维数灾难
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/12691342.html
Copyright © 2011-2022 走看看