static String domainOf(String url) {
    int start = -1;
    if (url.startsWith("//")) {
      start = 2;
    } else {
      start = url.indexOf("://");
      if (start >= 0) { start += 3; }
    }
    if (start < 0) { return null; }
    for (int i = 0; i < start - 3; ++i) {
      switch (url.charAt(i)) {
      case '/': case '?': case '#': return null;
      default: break;
      }
    }
    int end = url.length();
    for (int i = start; i < end; ++i) {
      switch (url.charAt(i)) {
      case '/': case '?': case '#': end = i; break;
      default: break;
      }
    }
    if (start < end) {
      return url.substring(start, end);
    } else {
      return null;
    }
  }
static String domainOf ( String url ) { int start = - 1 ; if ( url . startsWith ( "//" ) ) { start = 2 ; } else { start = url . indexOf ( "://" ) ; if ( start >= 0 ) { start += 3 ; } } if ( start < 0 ) { return null ; } for ( int i = 0 ; i < start - 3 ; ++ i ) { switch ( url . charAt ( i ) ) { case '/' : case '?' : case '#' : return null ; default : break ; } } int end = url . length ( ) ; for ( int i = start ; i < end ; ++ i ) { switch ( url . charAt ( i ) ) { case '/' : case '?' : case '#' : end = i ; break ; default : break ; } } if ( start < end ) { return url . substring ( start , end ) ; } else { return null ; } }
The domain (actually authority component) of an HTML5 URL.
If the input is not hierarchical, then the return value is undefined.