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));

解决方案 »

  1.   

    String s = "http://www.sohu.com/mails/cgi-bin/news/index.jsp";
    String a[] = s.split("/");for (int i = 0; i < a.length; i ++)
    {
    System.out.println (a [i]);
    }想要哪个都可以!!
      

  2.   

    楼上的两位都不够通用化,我想楼主的意思是从一个url中取出远程主机名
    应该实现输入不管是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;
    }
      

  3.   

    不好意思,请去掉throws Exception