計(jì)算字符串中子串出現(xiàn)的位置時(shí)結(jié)果出現(xiàn)了兩次,可能原因是什么?/*計(jì)算字符串中子串出現(xiàn)的位置, */ 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 個(gè)答案
答案 1:
把fromIndex = fromIndex+ s1.lengt-();改為fromIndex = + s1.lengt-();