public class StringTest1 {
public static void main(String[] args) {
System.out.println("获取一段字符串在指定的字符串中出现的次数");
String yoursecret = "fsdkfhsecretsdfsecretsndsecretshedfhsecret";
String secret = "secret";
System.out.println(count2(secret, yoursecret));
}
public static int count2(String smallstring, String bigstring) {
int count = 0;
int i = bigstring.indexOf(smallstring);
while(i != -1){
bigstring = bigstring.substring(i + smallstring.length());
count++;
i = bigstring.indexOf(smallstring);
}
return count;
}
} |
|