getting host from URL string

Posted on 2009-12-17
Last Modified: 2012-05-08
I have a url string and I'd like to get the host out of it.

Now, I know I can just do:

URL url = new URL("http:///");
String host = url.getHost();

But must I really instatiate a URL every time?  Is there some kind of a static utility class that would do this without the need to do "new URL" ?  For the purposes of this question, assume that I don't want to be manually parsing the string using a regex or a substring.
Question by:aturetsky
    LVL 86

    Accepted Solution

    No - that's the way to do it
    LVL 3

    Assisted Solution

    What is your problem with creating URL objects? Have you got any evidence that such creation will cause you problems? If not, bare in mind Donald Knuth's wise warning: "premature optimization is the root of all evil". Follow CEHJ's advice.
    LVL 92

    Assisted Solution

    > But must I really instatiate a URL every time?

    You don't need to but its the simplest
    the other alternative as you mention is to parse the string, which may be worth considering if you had a *lot* of url's you wanted to get the host from

    > assume that I don't want to be manually parsing the string using a regex or a substring.

    thats what a static utility method would have to do :)
    LVL 1

    Author Closing Comment

    Thanks to all you.  I must admit it's rare to see this kind of consensus here - so this must be the way to go, then.
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    EvenOdd challenge 10 67
    nested class vs inner class 5 36
    count8 challlenge 13 67
    pairs challenge 5 29
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now