老师那样写确实不怎好看,这是我写的,应该比老师写的容易看些
public class StringTest {
static int count = 0;
public static void main(String[] args) {
String s = "sdkkffkksfskkdfskkdfsdkk";
String s1 = "kk";
getZiString(s, s1);
System.out.println(count);
}
public static int getZiString(String str1, String str2) {
int index = 0;
while ((index = str1.indexOf(str2)) != -1) {
str1 = str1.substring(index + str2.length(), str1.length());
count++;
}
return count;
}
} |