public int getTimes(String str1, String str2) {
int count = 0;
int start;
while ((start = (str1.indexOf(str2))) >= 0) {
count++;
str1 = str1.substring(start + str2.length());
}
return count;
}
str1是长字符串,str2是短字符串。上面方法返回值即为长字符串中有几个短字符串。