开发者

Slash before InetAddress.getByName(host)

How do I remove the slash in the output of InetAddress.getbyName?


UPDATE

Thanks everyone,开发者_如何学Go I just did it.

One of the solutions is:

String ip_old = myInetaddress.toString(); 
String ip_new = ip_old.substring(1); 


If you just want the IP, use the host address:

String address = InetAddress.getByName("stackoverflow.com").getHostAddress();

If you just want the host name, use

String hostname = InetAddress.getByName("stackoverflow.com").getHostName();

Edit

The slash you're seeing is probably when you do an implicit toString() on the returned InetAddress as you try to print it out, which prints the host name and address delimited by a slash (e.g. stackoverflow.com/64.34.119.12). You could use

String address = InetAddress.getByName("stackoverflow.com").toString().split("/")[1];
String hostname = InetAddress.getByName("stackoverflow.com").toString().split("/")[0];

But there is no reason at all to go to a String intermediary here. InetAddress keeps the two fields separate intrinsically.


I'm assuming you are doing a toString after this? Why don't you just use normal String manipulation, meaning substring?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜