gpt4 book ai didi

java - InetAddress.toString() 返回正斜杠

转载 作者:太空狗 更新时间:2023-10-29 22:52:06 25 4
gpt4 key购买 nike

我有一个 DatagramPacket 类型的变量包。虽然 packet.getAddress().toString() 生成一个表示 IP 地址的字符串,但它在字符串的开头附加了一个额外的 /:

/127.0.0.1

我可以轻松删除前导的 '/',但是是否有更好的方法来获取 IP 的字符串表示形式?我很担心,因为如果在其他情况下有更多的 '/' 怎么办。

谢谢!

最佳答案

使用以下内容:

packet.getAddress().getHostAddress()

来自documentation :

Returns the IP address string in textual presentation.

对比 InetAddress.toString() :

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com