Reputation: 6606
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!
Upvotes: 1
Views: 2977
Reputation: 11408
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();
Upvotes: 7
Reputation: 37516
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).
Upvotes: 3