Java 查处所有特定字符所出现的位置

img


代码中给定字符串,运行后查询出所有非法内容所出现的位置。1111111111111111111111111

public static void main(String[] args) {
        String str = "1231211255121";
        int frontLength = 0; 
        while(str.contains("2")){ 
            int index = str.indexOf("2"); 
            System.out.println( "2出现位置"+index + frontLength + 1);
            frontLength += (index + 1);
 
            str = str.substring(index + 1);
        }
        System.out.println();
    }