Edmund Rojas
Edmund Rojas

Reputation: 6606

how to remove first two segments of a url string in java?

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

Answers (2)

Austyn Mahoney
Austyn Mahoney

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

wsanville
wsanville

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

Related Questions