1 public class Test { 2 public static void main(String[] args) { 3 String s = " xXccxxxXX " ; 4 // 从头开始查找是否存在指定的字符,索引从0开始 // 结果如下 5 System.out.println(s.indexOf( " c " )); // 2 6 // 从第四个字符位置开始往后继续查找,包含当前位置 7 System.out.println(s.indexOf( " c " , 3 )); // 3 8 // 若指定字符串中没有该字符则系统返回-1 9 System.out.println(s.indexOf( " y " )); // -1 10 System.out.println(s.lastIndexOf( " x " )); // 6 11 } 12 }