Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7641323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:53:35+00:00 2026-05-31T08:53:35+00:00

Given a URL, I want to extract domain name(It should not include ‘www’ part).

  • 0

Given a URL, I want to extract domain name(It should not include ‘www’ part). Url can contain http/https. Here is the java code that I wrote. Though It seems to work fine, is there any better approach or are there some edge cases, that could fail.

public static String getDomainName(String url) throws MalformedURLException{
    if(!url.startsWith("http") && !url.startsWith("https")){
         url = "http://" + url;
    }        
    URL netUrl = new URL(url);
    String host = netUrl.getHost();
    if(host.startsWith("www")){
        host = host.substring("www".length()+1);
    }
    return host;
}

Input: http://google.com/blah

Output: google.com

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:53:37+00:00Added an answer on May 31, 2026 at 8:53 am

    If you want to parse a URL, use java.net.URI. java.net.URL has a bunch of problems — its equals method does a DNS lookup which means code using it can be vulnerable to denial of service attacks when used with untrusted inputs.

    “Mr. Gosling — why did you make url equals suck?” explains one such problem. Just get in the habit of using java.net.URI instead.

    public static String getDomainName(String url) throws URISyntaxException {
        URI uri = new URI(url);
        String domain = uri.getHost();
        return domain.startsWith("www.") ? domain.substring(4) : domain;
    }
    

    should do what you want.


    Though It seems to work fine, is there any better approach or are there some edge cases, that could fail.

    Your code as written fails for the valid URLs:

    • httpfoo/bar — relative URL with a path component that starts with http.
    • HTTP://example.com/ — protocol is case-insensitive.
    • //example.com/ — protocol relative URL with a host
    • www/foo — a relative URL with a path component that starts with www
    • wwwexample.com — domain name that does not starts with www. but starts with www.

    Hierarchical URLs have a complex grammar. If you try to roll your own parser without carefully reading RFC 3986, you will probably get it wrong. Just use the one that’s built into the core libraries.

    If you really need to deal with messy inputs that java.net.URI rejects, see RFC 3986 Appendix B:

    Appendix B. Parsing a URI Reference with a Regular Expression

    As the “first-match-wins” algorithm is identical to the “greedy”
    disambiguation method used by POSIX regular expressions, it is
    natural and commonplace to use a regular expression for parsing the
    potential five components of a URI reference.

    The following line is the regular expression for breaking-down a
    well-formed URI reference into its components.

      ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
       12            3  4          5       6  7        8 9
    

    The numbers in the second line above are only to assist readability;
    they indicate the reference points for each subexpression (i.e., each
    paired parenthesis).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a URL that is http://www.example.com/sites/dir/index.html , I would want to extract
I want to extract the base name from a image URL in Javascript. Would
For a given url, I want to get the name-after-hash's age from the database.
I want to extract the values - Screen Name, Name, ID, Profile Picture URL
I want to redirect to the given url: http://www.silvertouch.mobi/mynino/ninoparent/paypalPayment.php ? by clicking the button
E.g: The given text with special URL which I want: 17.http://www.kt8.com.cn/images/skin/small/url.gifhttp://u.115.com/file/t16785f328# XTM.DVD-HALFCD2.Touch.1985.EP017.mkv 18.http://www.kt8.com.cn/images/skin/small/url.gifhttp://u.115.com/file/t1c64a6022# XTM.DVD-HALFCD2.Touch.1985.EP018.mkv
I have been given an url and I want to extract the contents of
I want to pop a browser with a given url from within a windows
Given a web URL, I want to detect all the links in a WEBSITE
Given a parent URL (say http://dir.yahoo.com/News_and_Media/ ), I want to scrape all URLs which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.