String str = "http://www.sohu.com/mails/cgi-bin/news/index.jsp";
int startPos = str.indexOf("http://");
int endPos = str.indexOf("/",startPos+7);
System.out.println(str.substring(startPos+7,endPos));
int startPos = str.indexOf("http://");
int endPos = str.indexOf("/",startPos+7);
System.out.println(str.substring(startPos+7,endPos));
String a[] = s.split("/");for (int i = 0; i < a.length; i ++)
{
System.out.println (a [i]);
}想要哪个都可以!!
应该实现输入不管是http://www.sohu.com/mails/cgi-bin/news/index.jsp
还是ftp://www.sohu.com/mails
……都能取得www.sohu.com,也就是“//”与第一个“/”之间或
import java.net.URL;String getHostFromUrl(String url) throws Exception {
String host = "";
try {
String s = new URL(url);
host = s.getHost();
}
catch (Exception e) {
e.printStackTrace();
} return host;
}