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("\""));
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With