從給定的URL獲取域名給定一個URL,我想提取域名(它不應該包含'www'部分)。網址可以包含http / https。這是我寫的java代碼。雖然它似乎工作正常,有沒有更好的方法或有一些邊緣情況,可能會失敗。public static String getDomainName(String url) throws MalformedURLException{
if(!url.startsWith("http") && !url.startsWith("https")){
url = "http://" + url;
}
URL netUrl = new URL(url);
String host = netUrl.getHost();
if(host.startsWith("www")){
host = host.substring("www".length()+1);
}
return host;}輸入:http://google.com/blah輸出:google.com
添加回答
舉報
0/150
提交
取消