米字键

博客园 首页 新随笔 联系 订阅 管理

      public class Work {

       /*
               *  获取一个字符串中,另一个字符串出现的次数
               *  思想:
               *    1. indexOf到字符串中到第一次出现的索引
               *    2. 找到的索引+被找字符串长度,截取字符串
               *    3. 计数器++
             */
            public static void main(String[] args) {

            String s="hellowrdhellowrdhellowrdhellowrdhellowrdhellowrd";     //定义字符串
                   function (s,"l");  //添加function方法

         }

        public  static void function(String s, String m) {  //调用function方法
                  int count=0;  定义 int类型的数量
                  while(s.indexOf(m)>=0){  //使while语句 定义查找字符或者子串第一次出现的地方从零开始
                      int  index=s.indexOf(m)+m.length();   //查找m的字符串和m字符串的长度
                      s=s.substring(index)        //  ;截取字符串
                      count++;              

        }
                    int q=s.length();      //字符串的长度


            System.out.println("出现的 索引 :"+count+"     字符串的长度:"+q);  //打印
        
    }

      }

posted on 2019-03-18 22:16  米字键  阅读(829)  评论(0编辑  收藏  举报