package string_学习;
public class test5 {
public static int getoccur(String src, String find) {
//getoccur("abchdjfhabcsdbakb", "abc")
int cs = 0;
int index = -1;
while ((index = src.indexOf(find, index)) > -1) {
++index;
++cs;
}
return cs;
}
public static void getoccur1(String src, String find) {
//getoccur1("abchdjfhabcsdbakb", "abc");
boolean cz = true; // 判断是否存在字串
if (!src.contains(find)) {
cz = false;
}
if (cz) {// 如果存在
int h = 0;
System.out.print("该子串的位置为:");
while (true) {
int pos = src.indexOf(find); //find=abc
if (pos == -1) {
break;
}
System.out.print((pos + h));// 字串位置0
System.out.print(" ");
src = src.substring(pos + find.length());// 截取位置 3
h += (pos + find.length());// h为截取的字符数 0+3
}
} else {
System.out.println("不包含该字串");
}