如何删除Jsoup输出段落中的段落之间的空格?

问题描述:

这是我的代码。当输出打印,也打印段落之间的白色空间。如何删除段落之间的空白,然后我想在数组列表中逐句存储。如何删除Jsoup输出段落中的段落之间的空格?

public static void main(String[] args) { 

    try { 
      String url = "http://www.divaina.com/"; 

      System.setProperty("http.proxyHost", "cache.mrt.ac.lk"); 
      System.setProperty("http.proxyPort", "3128"); 

      Document doc = Jsoup.connect(url).timeout(10000).get(); 

      Elements paragraphs = doc.select("p"); 
      for(Element p : paragraphs){ 
      System.out.println(p.text());} 
       } 
     catch (IOException ex) { 
      ex.printStackTrace(); 
      } 


} 

当我直接将内容添加到数据库空白处也添加它。我怎样才能删除段落之间的空白处?其实我想读网页的内容并逐行添加到数据库中。有没有其他适当的方法来做到这一点?

Screen shot of out come

显然,有些段落不包含文本。这可能有所帮助:

for (Element p : paragraphs) 
{ 
    if (p.text().length() != 0) 
    System.out.println(p.text()); 
} 
+0

除了一些内容几乎没有问题。 Thanx很多博士。祝你今天愉快 :) – Maduri 2014-11-22 13:18:58

使用正则表达式:

String withoutspace = whitespace.replaceAll("\\s", ""); 

或者试试这个

String withoutSpace = whitespace.replace("\n", "").replace("\r", ""); 
+0

段落之间出现空格。一段打印。然后有时打印空间,然后再打印下一个paragrph。上面的代码不适合我。 – Maduri 2014-11-22 10:01:06