Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

getting host from URL string

Posted on 2009-12-17
5
Medium Priority
?
273 Views
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:///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.
0
Comment
Question by:aturetsky
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 26075162
No - that's the way to do it
0
 
LVL 3

Assisted Solution

by:softwarepearls_com
softwarepearls_com earned 500 total points
ID: 26075816
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
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 26076062
> 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
 
LVL 1

Author Closing Comment

by:aturetsky
ID: 31667498
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 26076104
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

572 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