1. 程式人生 > >indexOf()用法 示例如下。

indexOf()用法 示例如下。

static keyword 字符串 tin lastindex xxx cxx ast tar

  1. public class Test {
  2. public static void main(String[] args) {
  3. String s = "xXccxxxXX";
  4. // 從頭開始查找是否存在指定的字符 //結果如下
  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. }

indexOf()用法 示例如下。