user5396187
user5396187

Reputation:

How to filter URL on the bases of web domain?

I have set of urls now i want to filter them out on the bases of web domains(say wikipedia urls). Right now what i am doing is iterating set and for each url i am just finding a keyword of that web address.

 if(ur.contains("wikipedia.org")){
    //do something
 }

is there any other technique that is more efficient than my current approach?

Upvotes: 5

Views: 319

Answers (2)

Ivan
Ivan

Reputation: 3062

Viartemev's answer is good if you need to get full domain (e.g. someinfo.wikipedia.org) If you want to get top level domain only (e.g. wikipedia.org) then .contains() is the best approach

if(url.contains("wikipedia.org")){
    domain = wikipedia.org"
 }

Upvotes: 0

viartemev
viartemev

Reputation: 301

You can use this:

if("wikipedia.org".equals(getDomainName(ur))){
    //do something
 }

public static String getDomainName(String url) throws URISyntaxException {
    URI uri = new URI(url);
    String domain = uri.getHost();
    return domain.startsWith("www.") ? domain.substring(4) : domain;
}

Upvotes: 2

Related Questions