我有一个 DatagramPacket
类型的变量包。虽然 packet.getAddress().toString()
生成一个表示 IP 地址的字符串,但它在字符串的开头附加了一个额外的 /
:
/127.0.0.1
我可以轻松删除前导的 '/'
,但是是否有更好的方法来获取 IP 的字符串表示形式?我很担心,因为如果在其他情况下有更多的 '/'
怎么办。
谢谢!
最佳答案
使用以下内容:
packet.getAddress().getHostAddress()
来自documentation :
Returns the IP address string in textual presentation.
Converts this IP address to a
String
. The string returned is of the form: hostname / literal IP address. If the host name is unresolved, no reverse name service lookup is performed. The hostname part will be represented by an empty string.
关于java - InetAddress.toString() 返回正斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12947435/