13,100
社区成员
发帖
与我相关
我的任务
分享
/**
* 字符在字符串中出现的次数
*
* @param string
* @param a
* @return
*/
public static int occurTimes(String string, String a) {
int pos = -2;
int n = 0;
while (pos != -1) {
if (pos == -2) {
pos = -1;
}
pos = string.indexOf(a, pos + 1);
if (pos != -1) {
n++;
}
}
return n;
}