views:

209

answers:

2

Hi ,

Given this :

URL u=new URL("someURL");

How do i identify the top level domain of the URL..

+1  A: 

Use URL#getHost() and if necessary thereafter a String#split() on "\\.".

Update: if you actually have an IP address as host, then you need to make use of InetAddress#getHostName() independently.

BalusC
String[] htokens=u.getHost().toString().split("."); is there anything wrong with this line.. Because ,after this statement , the length of htokens array still remains 0 though u.getHost().toString() returns "a.bc.de" Please help
trinity
The link mentions that it takes a regex pattern. The answer mentions that you need to split on `"\\."`.
BalusC
+1  A: 

The host part of the url conforms to RFC 2732 according to the docs. It would imply that simply splitting the string you get from

  String host = u.getHost();

would not be enough. You will need to ensure that you conform to the RFC 2732 when searching the host OR if you can guarantee that all addresses are of the form server.com then you can search for the last . in the string and grab the tld.

Vincent Ramdhanie