java.lang.StringIndexOutOfBoundsException: String index out of range: -1出现这个问题,在eclipse里指出出错的行数,可是我不知道为什么出错,我用的是substring函数,参数的beginIndex为一个html文件中一个字符串的位置String content3 = content.substring(content.indexOf("<head"));提示是这一行出错,可是我不知道错误在哪?怎么解决呢?

解决方案 »

  1.   

    如果content中不包含"<head"的话会出错
      

  2.   

    你看看content.indexOf("<head")值是多少,然后看看content中有没有"<head",多数没有,返回的值应该是-1,所以你在substring时出错
      

  3.   

    我在一个html文件中获取<head>到文件结尾的所有字符html里有<head>这个字符串的,可是为什么substring时出错呢?
      

  4.   

    用System.out.println打印出你的content内容先
      

  5.   

    String content3 = null;
    int pos = content.indexOf("<head");
    if (pos > -1) content3 = content.substring(pos);