0

更新

好像当我从这个问题中复制字符串"       AAAAAA"并替换它时。该程序正常工作。

所以我只是通过改回"       AAAAAA"我以前的错误字符串来做一些测试。然后删除所有空格并通过键入手动输入它,它可以工作!?!?。

这是我的完整代码

public static void main(String[] args) {
    URL url;
    InputStream is = null;

    try {
        url = new URL(
                "http://writer.dek-d.com/nattione/story/viewlongc.php?id=466201&chapter=966");

        is = url.openStream(); // throws an IOException
        String result = convertStreamToString(is);
        createFictionFiles(result);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // nothing to see here
        }
    }
}

public static void createFictionFiles(String html) {
    Document doc = Jsoup.parse(html);
    Element title = doc.select("title").first();
    Element table = doc.select("#story_body").first();
    
    String showTitle = "";
    String showStory = "";
    BufferedWriter bw;
    try {
        showTitle = new String(title.text());
        showStory = new String(table.text());
        //showStory = "       AAAAAA"; these spaces I copy it from log in eclipse. It is the spaces that come from web
        boolean find = showStory.contains("\\t");
        boolean find1 = showStory.contains("\\n");
        boolean find2 = showStory.contains("\\x0b");
        boolean find3 = showStory.contains("\\r");
        boolean find4 = showStory.contains("\\f");
        boolean find5 = showStory.contains(" ");
        boolean find6 = showStory.contains("  ");
        boolean find7 = showStory.matches("\\s");
        System.out.println("\\t = " + find);
        System.out.println("\\n = " + find1);
        System.out.println("\\x0b = " + find2);
        System.out.println("\\r = " + find3);
        System.out.println("\\f = " + find4);
        System.out.println(" = " + find5);
        System.out.println("  = " + find6);
        System.out.println("\\s = " + find7);

        File file = new File("D:/" + "test"
                + ".txt");
        
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        bw = new BufferedWriter(fw);
        bw.write(showStory);
        bw.close();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
}

public static String convertStreamToString(InputStream is) {
    Scanner s = new Scanner(is, "TIS-620").useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

老的

我有这个字符串

showStory = "       AAAAAA";

这些字符串是从 Web 检索并使用库将其解析为字符串格式

我想将超过 3 英寸的空间分开,将它们替换为"\n"

我有这些测试。

boolean find = showStory.contains("\\t");
boolean find1 = showStory.contains("\\n");
boolean find2 = showStory.contains("\\x0b");
boolean find3 = showStory.contains("\\r");
boolean find4 = showStory.contains("\\f");
boolean find5 = showStory.contains(" ");
boolean find6 = showStory.contains("  ");
boolean find7 = showStory.contains("\\s");

结果是

\t = false
\n = false
\x0b = false
\r = false
\f = false
 = true
  = false
\s = false

我不知道为什么在我的字符串中有 8 个空格的情况下,为什么会有多个空格给我 false。我也将方法从更改为containsmatches但所有结果都是错误的。

谁能帮我这个?

谢谢。

4

1 回答 1

3

请再次检查...它不可能识别出 find6 中的 2 个空格

 String showStory = "       AAAAAA";

 boolean find = showStory.contains("\\t");
 boolean find1 = showStory.contains("\\n");
 boolean find2 = showStory.contains("\\x0b");
 boolean find3 = showStory.contains("\\r");
 boolean find4 = showStory.contains("\\f");
 boolean find5 = showStory.contains(" ");
 boolean find6 = showStory.contains("  ");
 boolean find7 = showStory.contains("\\s");

 System.out.println("find ="+find);
 System.out.println("find1 ="+find1);
 System.out.println("find2 ="+find2);
 System.out.println("find3 ="+find3);
 System.out.println("find4 ="+find4);
 System.out.println("find5 ="+find5);
 System.out.println("find6 ="+find6);
 System.out.println("find7 ="+find7);


  }//end of main

这是印刷品:

find =false
find1 =false
find2 =false
find3 =false
find4 =false
find5 =true
find6 =true
find7 =false
于 2013-02-09T19:30:41.650 回答