Extract base url from full url

前端 未结 3 1962
小鲜肉
小鲜肉 2021-02-08 10:42

Any quick way to extract base url from full url? for e.g., if i have http://test.example.com/abcd/test.html - i want only http://test.example.com.

I can always do strin

3条回答
  •  野的像风
    2021-02-08 11:13

    What about:

    import java.net.URL;
    import java.net.MalformedURLException;
    
    try
    {
      URL url = new URL("http://test.example.com/abcd/test.html");
      String baseUrl = url.getProtocol() + "://" + url.getHost();
    }
    catch (MalformedURLException e)
    {
      // do something
    }
    

提交回复
热议问题