可以考虑用try ...catch 子句捕捉一下错误,然后不做任何处理就行了。

解决方案 »

  1.   

    首先,还是要看看错误发生的原因,严谨些嘛!
        一般string容易出StringIndexOutOfBoundsException的问题,往往在循环等中出现。如果非要忽略,就这样
    try {
      String.indexOf(string);
    }
    catch(Exception e){
      System.out.println(e.toString());
    }^^^^^^^
    这样就可以忽略了
      

  2.   

    if(str1.indexOf(searchStr) == -1) {  // not found
      ...
    }else {
      ...
    }
      

  3.   

    我写了个小程序测试了一下,好像并不会中断程序。
    程序如下:
    package com.CSDN.String;public class TestString {
      public static void main(String[] args) {
        String testString = "i love java";
        int returnValue = testString.indexOf("aaa");
        System.out.println(returnValue);
        returnValue = testString.indexOf("a");
        System.out.println(returnValue);
      }
    }运行结果:
    -1
    8