if I have a url string like "http://www.google.com" how can I convert that to just "google.com" in java? Im assuming there is a way to parse this using a function in java, any help will go a long way thanks!
Asked
Active
Viewed 2,781 times
1
-
http://stackoverflow.com/questions/2993649/how-to-normalize-a-url-in-java – Habib Apr 13 '12 at 21:22
2 Answers
7
Create a URL object and then use the methods provided to grab whatever you need.
Example:
URL myURL= new URL("http://www.google.com");
String host = myURL.getHost();

Austyn Mahoney
- 11,398
- 8
- 64
- 85
-
1Looks like that guy removed his hacky solution to this problem lol – JakeWilson801 Apr 19 '12 at 01:08
-
It was a 15 year old kid with a bunch of substring methods. I hope he at least took the advice we gave him. – Austyn Mahoney Apr 20 '12 at 19:04
-
3
Using the java.net.URL class, there's a method called getHost()
that will do exactly that.
It's much easier to use a built in method, rather than rolling your own (you need to be aware of multiple sub domains, different protocol like https).
-
1Thank you, at least someone else also has the sense to provide a REAL solution and not some hacky substring function. – Austyn Mahoney Apr 13 '12 at 21:27