可以用String的indexof(str,fromindex)方法,循环遍历加一个计数器统计次数。public class CountTimes {public static void main(String[] args) {String str="In the entire world there's nobody like me and my best wishes is become myself";int times = searchstr("my", str); //返回2...
java怎么实现统计一个字符串中字符出现的次数
可以用String的indexof(str,fromindex)方法,循环遍历加一个计数器统计次数。
public class CountTimes {public static void main(String[] args) {String str="In the entire world there's nobody like me and my best wishes is become myself";int times = searchstr("my", str); //返回2System.out.println(times); }public static int searchstr(String key, String str) {int index = 0;//每次的搜索到的下标int count = 0;//计数器while (( index=str.indexOf(key, index)) != -1) {index = index + key.length();count++;}return count; }}2017-02-23
把字符串截取成一个数组,然后遍历数组匹配某个字符串2017-02-22
循环中匹配2017-02-19