How to filter a URL based on a web domain?

I have a set of URLs, now I want to filter them based on web domains (e.g. Wikipedia URLs). Right now, what I'm doing is a set of iterations, and for each URL I just find the keyword of this web address.

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

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

+5
source share
2 answers

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; } 
+2
source

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

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

Source: https://habr.com/ru/post/1232687/


All Articles