将字符串里的123替换成空字符串,然后用原字符串长度减去替换后的字符串长度再除以3
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count = 0;
while(lastIndex != -1){
lastIndex = str.indexOf(findStr,lastIndex);
if(lastIndex != -1){
count ++;
lastIndex += findStr.length();
}
}
System.out.println(count);
网上搜索的,把hello替换成123就行
最直观的办法:
for循环,遍历,碰到1就往后检查两位是不是23,是就+1,不是的话要判断一下后一位是不是1,是1继续上面逻辑,不是跳过
还可以调用库函数,string.contains("123")
还有可以搜一下正则 regular match应该也可以
replaceall 123,然后把前后的字符串长度差除以3,结果就是123的数量