how-to-split-string-url-in-java

https://stackoverflow.com/questions/24607644/how-to-split-string-url-in-java:

URL u = new URL(VAC_URL);
String host = u.getHost();
int port = u.getPort();

http://www.java2s.com/Tutorial/Java/0320__Network/URLSplitter.htm:

import java.net.URL;

public class MainClass {

  public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com:80/index.html");
    System.out.println("The URL is " + u);
    System.out.println("The scheme is " + u.getProtocol());
    System.out.println("The user info is " + u.getUserInfo());

    String host = u.getHost();
    if (host != null) {
      int atSign = host.indexOf('@');
      if (atSign != -1)
        host = host.substring(atSign + 1);
      System.out.println("The host is " + host);
    } else {
      System.out.println("The host is null.");
    }

    System.out.println("The port is " + u.getPort());
    System.out.println("The path is " + u.getPath());
    System.out.println("The ref is " + u.getRef());
    System.out.println("The query string is " + u.getQuery());
  }

}
The URL is http://www.java2s.com:80/index.html
The scheme is http
The user info is null
The host is www.java2s.com
The port is 80
The path is /index.html
The ref is null
The query string is null
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容