1

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!

Edmund Rojas
  • 6,376
  • 16
  • 61
  • 92

2 Answers2

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
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).

Community
  • 1
  • 1
wsanville
  • 37,158
  • 8
  • 76
  • 101