计算字符串中子串出现的位置时结果出现了两次,可能原因是什么?/*计算字符串中子串出现的位置, */ class X{ public static void -in(String[] args){ String s = "abkkcdkkabkkefkk"; String s1 = "kk"; times(s, s1); } public static void times(String s, String s1){ int fromIndex = 0; w-ile(true){ int = s.Of(s1,fromIndex); if(!=-1){ System.out.println(); fromIndex = fromIndex+ s1.lengt-(); } } } }1 个答案
答案 1:
把fromIndex = fromIndex+ s1.lengt-();改为fromIndex = + s1.lengt-();