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 になる理由がわかりません。メソッドも からcontainsに変更しmatchesますが、すべての結果は false です。

誰でもこれで私を助けることができますか?

ありがとう。

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 に答える