//字符串里寻找另一个串出现的次数和第一次出现的位置。
@Test
public void findSubString(){
String s="IloveJavaloveC++lovePython";
String temp="love";
int index=s.indexOf(temp);
int counts=0;
if(index!=-1){
System.out.println(index);
s=s.substring(index+temp.length());
counts++;
}
else return;
while(true){
index=s.indexOf(temp);
if(index!=-1) {
counts++;
s=s.substring(index+temp.length());
}
else break;
}
System.out.println(counts);
}
本文介绍了一种使用Java实现的字符串匹配算法,该算法能够找出一个字符串在另一字符串中出现的次数及首次出现的位置。通过实例演示了如何使用indexOf方法进行子串定位,并采用循环结构实现多次查找。
1492

被折叠的 条评论
为什么被折叠?



