getting host from URL string

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:///whatever.com/whatever?whatever=whatever");
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.
LVL 1
aturetskyAsked:
Who is Participating?
 
CEHJCommented:
No - that's the way to do it
0
 
softwarepearls_comCommented:
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.
0
 
objectsCommented:
> 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 :)
0
 
aturetskyAuthor Commented:
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.
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.