【问题标题】:A certain string part needed需要某个字符串部分
【发布时间】:2013-11-21 22:05:08
【问题描述】:

我正在做一个应用程序,它应该将整个 website-html 文本放入字符串中。 然后我想使用 System.out.println 来显示该字符串的某个片段。我的代码

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {

        URL oracle = new URL("www.example-blahblahblah.com");
        BufferedReader in = new BufferedReader(
        new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)

       System.out.println(inputLine.substring(inputLine.indexOf("<section class=\"horoscope-content\"><p>")+1, inputLine.lastIndexOf("</p")));

        in.close();
    }
}

它应该显示下面键入的文本:

<section class="horoscope-content">
    <p>Text text text text</p>

而不是我有这个:

线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:-1 在 java.lang.String.substring(未知来源) 在 URLConnectionReader.main(URLConnectionReader.java:14)

我该怎么办?

【问题讨论】:

  • indexOflastIndexOf 如果找不到字符,则返回 -1
  • 您对 indexOf 的第二次调用返回 -1,这意味着未找到子字符串。打印整个字符串以查看其内容。我怀疑您在调用 substring 时要查找的文本被分成多行(因此分成不同的字符串)。
  • 您应该使用contains() 来检查短语,然后再将它们的位置用作索引。
  • 表示你匹配的字符串("

    ")。如果并且仅当找到您的字符串时,它将返回它的索引。否则它将始终返回-1。因为没找到,是不是打算在最后加上“

    ”?

  • 添加到@HunterMcMillen 的评论;第一步是验证您正在寻找的行是否确实存在于您从服务器获得的响应中。

标签: java


【解决方案1】:

您应该使用更宽容的正则表达式而不是indexOf,以便在对输入进行细微修改时更加稳定:

Pattern pattern = Pattern.compile("<section\\s+class\\s*=\\s*\"horoscope-content\"\\s*>\\s*<p>(.*?)</p>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
    System.out.println(matcher.group());
    System.out.println("Text in paragraph: " + matcher.group(1));
}

这将容忍换行符和其他空白字符。

【讨论】:

    【解决方案2】:

    您的代码每次检查 while 语句中的条件时都会重新分配 inputLine,具体取决于 HTML,您可能希望在查找标记部分之前读取整个文件。
    除非您确定 HTML 包含这些文本部分,否则当它不存在时,您仍然会遇到异常。
    您也只将开始的索引增加了 1,如果您不想要开始文本输出,则必须增加开始部分的长度。

    你可以试试这样的:

    StringBuilder html = new StringBuilder(); //holds all of the html we read
    String inputLine;
    while ((inputLine = in.readLine()) != null) { //read line by line
      html.append(inputLine); //add line to html
    }
    inputLine = html.toString(); //get 
    String startText = "<section class=\"horoscope-content\"><p>"; //starting tag
    int start = inputLine.indexOf(startText);
    int end = inputLine.lastIndexOf("</p"); //might want to use something like inputLine.indexOf("</p>", start); if there are multiple sections on the page
    if(start >= 0 && end >= 0) { //make sure we found a section
      System.out.println(inputLine.substring(start+startText.length(), end)); //print everything between the start and end tags (excluding the text in the start tag)
    } else {
      System.out.println("section not found"); //do something else since we didn't find the tags
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 2011-09-20
      相关资源
      最近更新 更多