string a=“csagmtacsmgtcs”;问计算cs的个数求大神代码
Pattern p = Pattern.compile("cs");
Matcher m = p.matcher("csagmtacsmgtcs");
int sum = 0;
while(m.find()) {
sum ++;
}
System.out.println(sum);
给楼上补下
public static void main(String args[]){
Pattern p = Pattern.compile("cs");
Matcher m = p.matcher("csagmtacsmgtcs");
int sum = 0;
while(m.find()) {
sum ++;
}
System.out.println(sum);
}
String a = "csagmtacsmgtcscs";
int sum = 0;
while (a.indexOf("cs") > -1) {
sum ++;
a = a.substring(a.indexOf("cs") + 2);
}
System.out.println(sum);
用string类的find那方法 找到就加一 没找到就结束