我正在使用bufferedreader提取5个网页,每个网页用空格分隔,我想使用子字符串来提取每个页面的url,html,source和date。但我需要有关如何正确使用子串来实现这一目标的指导,欢呼。
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 :(得分: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
答案 1 :(得分: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中使用正则表达式会替换包含正则表达式参数的字符串中的所有内容。