在InetAddress.getByName(host)之前斜杠 [英] Slash before InetAddress.getByName(host)
问题描述
如何删除 InetAddress.getbyName
输出中的斜杠?
How do I remove the slash in the output of InetAddress.getbyName
?
更新
谢谢大家,我刚刚做到了。
Thanks everyone, I just did it.
其中一个解决方案是:
String ip_old = myInetaddress.toString();
String ip_new = ip_old.substring(1);
推荐答案
如果您只想要IP,请使用主机地址:
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();
编辑
斜线是你的斜线当您尝试将其打印出来时,可能会在返回的 InetAddress
上执行隐式 toString()
时看到,打印由斜杠分隔的主机名和地址(例如 stackoverflow.com/64.34.119.12
)。您可以使用
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];
但是根本没有理由去 String
中介在这里。 InetAddress
保持这两个字段本质上是分开的。
But there is no reason at all to go to a String
intermediary here. InetAddress
keeps the two fields separate intrinsically.
这篇关于在InetAddress.getByName(host)之前斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!