尝试从缓冲读取器中提取子字符串,读取某些标记

我使用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;

}

回答:

在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字符串的一切。

回答:

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

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

以上是 尝试从缓冲读取器中提取子字符串,读取某些标记 的全部内容, 来源链接: utcz.com/qa/261773.html

回到顶部