java中如何高效获得字符串中特定字符的所有位置?

如题所述

第1个回答  2022-11-16
你试下下面的代码能满意吗 ,我已经试过了,可以的:\x0d\x0a public static void main(String args[]) {\x0d\x0aString str = "12&32&位置&yutye";\x0d\x0aSystem.out.println("&在字符串中出现的位置分别为:");\x0d\x0afor(int i=-1; i<=str.lastIndexOf("&");++i)\x0d\x0a{\x0d\x0a i=str.indexOf("&",i);\x0d\x0a System.out.print(i+"\t");\x0d\x0a \x0d\x0a} \x0d\x0a}
相似回答
大家正在搜