您的位置:首页 > 编程语言 > Java开发

java中统计一个字符串串在另一个字符串中出现的次数

2017-10-19 21:46 746 查看

package com.test.string;

public class StringUtils {

 //1.统计一个字符串在另一个字符串中出现的次数

 public static int stringCount(String str,String key){

  int index=0;

  int count=0;

  while((index=str.indexOf(key))!=-1){

   str=str.substring(index+key.length());

   count++;

  }

  

  return count;

 }

 

 public static void main(String[] args) {

  String str="adfdfdgdfdsfsdwerer";

  String key="d";

  int count=stringCount(str,key);

  System.out.println(count);

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐