Is there an easy way to convert String to Inetaddress in Java?

TiansHUo picture TiansHUo · Feb 22, 2010 · Viewed 20.4k times · Source

I am trying to convert strings into Inetaddress. I am not trying to resolve hostnames: the strings are ipv4 addresses. Does InetAddress.getByName(String host) work? Or do I have to manually parse it?

Answer

John Gardiner Myers picture John Gardiner Myers · Jun 15, 2012

com.google.common.net.InetAddresses.forString(String ipString) is better for this as it will not do a DNS lookup regardless of what string is passed to it.