作者:曾巧红-------- | 来源:互联网 | 2024-10-16 19:10
展开全部public class FindSubStringClass {*** @param args*public static void main(String[] args
展开全部
public class FindSubStringClass {
/**
* @param args
*/
public static void main(String[] args) {
32313133353236313431303231363533e4b893e5b19e31333264636165// TODO Auto-generated method stub
String str = “abcdabcdabcdabcd”;
String subStr = “abcd”;
int cursor = 0, subStrLen = subStr.length(), totalStrLen = str.length();
int count = 0; // 表示重复字符的个数
while ((cursor + subStrLen) <= totalStrLen) {
String tempStr = str.substring(cursor, cursor + subStrLen); // 获取字符串中的子字符串
if (tempStr.equals(subStr)) {
count++;
cursor += subStrLen;
} else {
cursor++;
}
}
System.out.println(subStr + &#8220;出现在&#8221; + str + &#8220;中的次数为&#8221; + count + &#8220;次&#8221;);
}
}