class Get {
/** 改变max */
int get1(String max, String min) {
int count = 0;
while (max.indexOf(min) != -1) {
max = max.substring((max.indexOf(min) + min.length()));
count++;
}
return count;
}
/** 改变max */
int get2(String max, String min) {
int count = 0;
int index = 0;
while ((index = max.indexOf(min)) != -1) {
max = max.substring(index + min.length());
count++;
}
return count;
}
/** 改变indexOf的起始索引位置 */
int get3(String max, String min) {
int count = 0;
int index = 0;
while ((index = max.indexOf(min, index)) != -1) {
index = index + min.length();
count++;
}
return count;
}
/** 改变max的长度 */
int get4(String max, String min) {
String temp = max.replace(min, "");
return (max.length() - temp.length()) / min.length();
}
/** 改变max的尾部 */
int get5(String max, String min) {
String temp = max.concat("不同于min");
String[] s = temp.split(min);
return s.length - 1;
}
}
public class 子串个数 {
public static void main(String[] args) {
String s1 = "11yy23yy54yy45yyyy4545yyyyyyyy";
String s2 = "yy";
System.out.println(new Get().get1(s1, s2));
System.out.println(new Get().get2(s1, s2));
System.out.println(new Get().get3(s1, s2));
System.out.println(new Get().get4(s1, s2));
System.out.println(new Get().get5(s1, s2));
}
}
|
|