Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring of a link String

Tags:

java

string

I would like to get only the actual link of the following link string:

String link = <a href="http://www.facebook.com/wwwausedu" target="_blank" class="btnFacebook">Link to Facebook</a>

The result should be only www.facebook.com/wwwausedu

I tried the following but it is not working:

TEMP = link.substring(link.indexOf("http://")+1, tmp.lastIndexOf("\""));
like image 213
Haya Raed Avatar asked Apr 20 '26 00:04

Haya Raed


2 Answers

You don't need the last index of ", but the first one after your http://:

TEMP = link.substring(link.indexOf("http://")+7, link.indexOf("\"", link.indexOf("http://")));

The String.indexOf(String str, int fromIndex) function gets the first occurence of str after the specified index. Also, as pointed out by @mellamokb the Wise, you need to add 7 to the index, not 1, since you want to exclude http:// from the result.

like image 102
mgibsonbr Avatar answered Apr 22 '26 12:04

mgibsonbr


Why not use tool specially designed for parsing HTML like jsoup.

String link = "<a href=\"http://www.facebook.com/wwwausedu\" "
        + "target=\"_blank\" class=\"btnFacebook\">Link to Facebook</a>";

Document doc = Jsoup.parse(link);
String address = new URL(doc.select("a").attr("href")).toString();

This will return: http://www.facebook.com/wwwausedu but we just want part without protocol so lets use URL now

URL url=new URL(address);
System.out.println(url.getHost()+url.getPath());

Output:

www.facebook.com/wwwausedu
like image 44
Pshemo Avatar answered Apr 22 '26 12:04

Pshemo