2013-01-04 39 views
0

我使用bufferedreader提取5个网页,每个网页用空格分隔,我想使用子字符串来提取每个网页url,html,源和日期。但我需要指导如何正确使用子字符串来实现这一点,欢呼声。尝试从缓冲读取器中提取子字符串,读取某些标记

public static List<WebPage> readRawTextFile(Context ctx, int resId) { 

    InputStream inputStream = ctx.getResources().openRawResource(
      R.raw.pages); 

    InputStreamReader inputreader = new InputStreamReader(inputStream); 
    BufferedReader buffreader = new BufferedReader(inputreader); 
    String line; 
    StringBuilder text = new StringBuilder(); 

    try { 
     while ((line = buffreader.readLine()) != null) { 


      if (line.length() == 0) {  
       // ignore for now 
           //Will be used when blank line is encountered 
      } 

      if (line.length() != 0) { 
     //here I want the substring to pull out the correctStrings 
       int sURL = line.indexOf("<!--"); 
        int eURL = line.indexOf("-->"); 
       line.substring(sURL,eURL); 
       **//Problem is here** 
      } 
     } 
    } catch (IOException e) { 
     return null; 

    } 
    return null; 
} 
+0

我想如何提取文本是这样的地址我想删除标签<! - 地址:http://www.google.co.uk.html-->所以,我离开了有了这个,我可以存储它:http://www.google.co.uk.html – rtkgpe

+0

为什么你想要通过子串操作?只需使用String.replace()。 – Smit

回答

0

在catch块不return null,使用printStackTrace();。它会帮助你找出是否出了问题。

 String str1 = "<!--Address:google.co.uk.html-->"; 
     // Approach 1 
     int st = str1.indexOf("<!--"); // gives index which starts from < 
     int en = str1.indexOf("-->"); // gives index which starts from - 
     str1 = str1.substring(st + 4, en); 
     System.out.println(str1); 

     // Approach 2 
     String str2 = "<!--Address:google.co.uk.html-->"; 
     str2 = str2.replaceAll("[<>!-]", ""); 
     System.out.println(str2); 

注$ 100:知道,在的replaceAll使用正则表达式它将取代含正则表达式PARAMS字符串的一切。

+0

谢谢,我需要能够从bufferedreader中提取地址。所以它会经过并找到文本文件中的每个地址取掉标签并返回地址 – rtkgpe

+0

@ rob12243我不明白。无论如何,你可以使用任何逻辑来实现你的目标。 – Smit

1

我觉得你想要的是这样的,

public class Test { 
    public static void main(String args[]) { 
    String text = "<!--Address:google.co.uk.html-->"; 
    String converted1 = text.replaceAll("\\<!--", ""); 
    String converted2 = converted1.replaceAll("\\-->", ""); 
    System.out.println(converted2); 
    } 

}

结果显示:地址:google.co.uk.html

+0

谢谢,我会看看我是否能适应它,所以我可以拯救5个网站。 – rtkgpe

+0

正如您使用'ReplaceAll()'。那为什么要这两个转换。你可以使用'regex'来达到同样的效果。无论如何。 – Smit

+0

@smit是的你没错。感谢指教:) – 9ine