{
public static void main(String[] args)
{
String srcText = "cx8923ulfk;dji-abc321fckdu9a-832abc-894ofjdksau9-83791piufdsiaabcfodsa";
String str = "abc";
int hits = 0;
int idx = 0 ;
for( int i = 0; i < srcText.length(); i ++)
{
if(srcText.charAt(i) == str.charAt(hits))
{
hits += 1;
if( hits == str.length())
{
idx = i - str.length() + 1;
hits = 0;
}
}
else
hits = 0;
}
System.out.println(idx);
System.out.println(srcText.lastIndexOf("abc"));
}
} idx是我定义的最后一次出现的位置,可是循环执行到倒数第二次就完了,问题出在哪了呢?