Java:判断字符串中包含某字符的个数

Java:判断字符串中包含某字符的个数

JAVA中查询一个词在内容中出现的次数:

public int getCount(String str,String key){  
  if(str == null || key == null || "".equals(str.trim()) || "".equals(key.trim())){  
    return 0;  
  }  
  int count = 0;  
  int index = 0;  
  while((index=str.indexOf(key,index))!=-1){  
    index = index+key.length();  
    count++;  
  }  
  return count;  
}  

 

posted @ 2018-03-08 13:28  整合侠  阅读(10097)  评论(0编辑  收藏  举报