What is the fastest way to get the domain/host name from a URL?

前端 未结 8 943
不知归路
不知归路 2020-12-08 15:48

I need to go through a large list of string url\'s and extract the domain name from them.

For example:

http://www.stackoverflow.com/questions

相关标签:
8条回答
  • 2020-12-08 16:37

    You could write a regexp? http:// is always the same, and then match everything until you get the first '/'.

    0 讨论(0)
  • 2020-12-08 16:38

    Assuming that they're all well-formed URLs, but you dont' know whether they'll be http://, https://, etc.

    
    int start = theUrlString.indexOf('/');
    int start = theUrlString.indexOf('/', start+1);
    int end = theUrlString.indexOf('/', start+1);
    String domain = theUrlString.subString(start, end);
    
    
    0 讨论(0)
提交回复
热议问题