zoukankan      html  css  js  c++  java
  • 二十五、JavaScript之查找字符串中的字符串indexOf和lastIndexOf的用法

    一、代码如下

    二、效果如下

    <!DOCTYPE html>
    <html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <body>
    <h1>我的第一张网页</h1>
    
    <p id="demo"></p>
    
    <script>
    var txt = "曾经沧海难为水,除却巫山不是云。曾经沧海难为水,除却巫山不是云。";
    var pos1 = txt.indexOf("沧海");
    var pos2 = txt.indexOf("沧海" , 5);
    var pos3 = txt.lastIndexOf("沧海");
    //LastIndexOf的第二个参数表上,从这个字符开始,向前查询
    var pos4 = txt.lastIndexOf("沧海" , 15);
    document.getElementById("demo").innerHTML = pos1 + ' - ' + pos2 + ' - ' + pos3 + ' - ' + pos4;
    </script>
    
    </body>
    </html>
  • 相关阅读:
    pyinstaller guid
    python filter()和map()函数
    python eval()
    day6
    repr()函数是什么鬼?
    fibonacci_question
    冒泡算法
    python 函数
    day4作业
    NOIp 2013 #1 积木大赛 Label:有趣的模拟
  • 原文地址:https://www.cnblogs.com/tianpan2019/p/11314760.html
Copyright © 2011-2022 走看看