Итак, вы хотите, чтобы часть домена верхнего уровня только?
//parameter urlString: a String
//returns: a String representing the TLD of urlString, or null iff urlString is malformed
private String getTldString(String urlString) {
URL url = null;
String tldString = null;
try {
url = new URL(urlString);
String[] domainNameParts = url.getHost().split("\\.");
tldString = domainNameParts[domainNameParts.length-1];
}
catch (MalformedURLException e) {
}
return tldString;
}
Давайте проверим это!
@Test
public void identifyLocale() {
String ukString = "http://www.amazon.co.uk/Harry-Potter-Sheet-Complete-Series/dp/0739086731";
logger.debug("ukString TLD: {}", getTldString(ukString));
String deString = "http://www.amazon.de/The-Essential-George-Gershwin/dp/B00008GEOT";
logger.debug("deString TLD: {}", getTldString(deString));
String ceShiString = "http://例子.测试";
logger.debug("ceShiString TLD: {}", getTldString(ceShiString));
String dokimeString = "http://παράδειγμα.δοκιμή";
logger.debug("dokimeString TLD: {}", getTldString(dokimeString));
String nullString = null;
logger.debug("nullString TLD: {}", getTldString(nullString));
String lolString = "lol, this is a malformed URL, amirite?!";
logger.debug("lolString TLD: {}", getTldString(lolString));
}
Выход:
ukString TLD: uk
deString TLD: de
ceShiString TLD: 测试
dokimeString TLD: δοκιμή
nullString TLD: null
lolString TLD: null