分别用String类中提供的方法和正则实现统计查找字符串中出现的指定字符的个数,例如:查找“jdflajfaldjfadf你好,,,你好,dfad”中“你好”的个数
public static int stringFunc(String str1,String str2) {
int count = 0;
while(str1.contains(str2)) {
int index = str1.indexOf(str2);
str1 = str1.substring(index + str2.length());
count++;
}
return count;
}
public static void main(String[] args) {
System.out.println(stringFunc("jdflajfaldjfadf你好,,,你好,dfad","你好"));
}