Android URL Parse getHost(String url)

Here you can find the source of getHost(String url)

Description

Will take a url such as http://www.stackoverflow.com and return www.stackoverflow.com Author: aioobe http://stackoverflow.com/a/4826122/1271424

License

Open Source License

Parameter

Parameter Description
url url need to find host

Return

host name of given url

Declaration

public static String getHost(String url) 

Method Source Code

//package com.java2s;
//  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of

public class Main {
    /**//from   w  w  w.  j av  a  2  s  .  co m
     * Will take a url such as http://www.stackoverflow.com and return www.stackoverflow.com
     * Author: aioobe http://stackoverflow.com/a/4826122/1271424
     *
     * @param url url need to find host
     * @return host name of given url
     */
    public static String getHost(String url) {
        if (url == null || url.length() == 0)
            return "";

        int doubleslash = url.indexOf("//");
        if (doubleslash == -1)
            doubleslash = 0;
        else
            doubleslash += 2;

        int end = url.indexOf('/', doubleslash);
        end = end >= 0 ? end : url.length();

        int port = url.indexOf(':', doubleslash);
        end = (port > 0 && port < end) ? port : end;

        return url.substring(doubleslash, end);
    }
}

Related

  1. parseHttpParamsToHash(String s)
  2. getNormalizedURLString(String url)
  3. getOrigin(String url)
  4. getParentUrl(String url)
  5. getRecentMediaUrl(String tag)