首页 > 其他 > 详细

Indexof

时间:2018-11-01 10:04:10      阅读:141      评论:0      收藏:0      [点我收藏+]

用indexof查找一串字符中某个字符出现的次数:

<!DOCTYPE html>
<html>
 <head>
     <meta charset="utf-8">
        <title>Indexof</title>   
    </head>
    <body>
     <button onclick="myFunction()">点我可查看这一串"one,two,three,one,one,two,three"字符中"one"字符出现的次数</button>   
        <p id="demo"></p>
        <script type="text/javascript">
         function myFunction(){
             var str = "one,two,three,one,one,two,three";
                var count=0;
                var index=0;
                var key="one";
                while((index = str.indexOf(key,index))!= -1){
                 index += key.length;
                    count = count+1;
                }
                document.getElementById("demo").innerHTML = count;
            }
        </script>   
    </body>
</html>

Indexof

原文:https://www.cnblogs.com/X1604389100/p/9887095.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!