I just want to show the website name only.
I don't want to show ".com" or "us.cnn.com" or "www.bbc.co.uk" Just the name of the website Like "cnn" or "bbc" only.
My code:
private String getHostName(String urlInput) {
urlInput = urlInput.toLowerCase();
String hostName = urlInput;
if (!urlInput.equals("")) {
if (urlInput.startsWith("http") || urlInput.startsWith("https")) {
try {
URL netUrl = new URL(urlInput);
String host = netUrl.getHost();
if (host.startsWith("www")) {
hostName = host.substring("www".length() + 1);
} else {
hostName = host;
}
} catch (MalformedURLException e) {
hostName = urlInput;
}
} else if (urlInput.startsWith("www")) {
hostName = urlInput.substring("www".length() + 1);
}
return hostName;
} else {
return "";
}
}
Inputs
http://www.bbc.co.uk/news/world-us-canada-39018776"
http://us.cnn.com/2017/02/18/politics/john-mccain-donald-trump-dictators/index.html"
http://bigstory.ap.org/article/d5dd5962fc4d42b195117ca63e0ba9af/revived-rally-trump-turns-back-governing
Outputs
www.bbc.co.uk
us.cnn.com
bigstory.ap.org
I just want to extract the "bbc", "cnn" and "ap" name from it.