要求:URL解析程序不停止循环
- 我需要接受一个字符串作为参数
- 打印出的第一个链接就找到
- 返回一个整数,是链接的指数End或-1如果 找不到链接。
如果if语句没有在字符串中找到“href”,它将返回整数,一个if语句在substring方法将不会获得索引超出边界错误之前返回-1。
显示需要返回字符串中的所有http站点。
我相信我是非常密切的,这里是我的代码:
public class LinkList {
public static void main(String[] args) {
String htmlCode = (a long string with all my url links)
int linkStart = 0;
int endLink = 0;
while (true) {
linkStart = hrefSearch(htmlCode);
if(linkStart == -1) break;
htmlCode = htmlCode.substring(linkStart);
}
}
public static int hrefSearch (String inStr) {
String find = " href=";
String closingBracket = ">";
int index = inStr.indexOf(find);
if (index != -1){
int closing = inStr.indexOf(closingBracket, index);
if (closing != -1){
System.out.println(inStr.substring (index, closing + 1));
}
}
return index;
}}
我怎样才能得到我的程序停止循环,并显示在我的字符串的所有网址?
您应该先阅读帮助中心。你的问题主体和标题有很多噪音,但没有真正的问题。 – 2014-10-08 05:06:22
你的代码产生了什么输出?什么不行? – Eran 2014-10-08 05:08:03
不会''href =“'更合适吗? – MadProgrammer 2014-10-08 05:08:08