java 在java(android)中将字符串转换为IP地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6026426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting string to IP Address in java(android)
提问by Mustehsan Ikram
When i convert string ("192.168.0.105")
to InetAddress in java (android). I am getting "/192.168.0.105"
. An extra "/"
is coming in InetAddress, which causes the socket not to be created.
当我("192.168.0.105")
在 java (android) 中将字符串转换为 InetAddress 时。我得到"/192.168.0.105"
. "/"
InetAddress 中有一个额外的内容,这会导致无法创建套接字。
How do i get rid of "/".
我如何摆脱“/”。
Regards,
问候,
Syed Mustehsan Ikram
Syed Mustehsan Ikram
回答by Harry Joy
You can use getHostAddress()
method of InetAddressto get host address without /
.
您可以使用InetAddress 的getHostAddress()
方法来获取主机地址,而无需./
And if you are using InetSocketAddress
then use getAddress().getHostAddress()
to get host ip without /
.
如果您正在使用InetSocketAddress
thengetAddress().getHostAddress()
来获取主机 ip 而不使用/
.
InetAddress inetAddress = InetAddress.getByName("192.168.0.105");
System.out.println(inetAddress.getHostAddress());
InetSocketAddress address = new InetSocketAddress("192.168.0.105", 5555);
System.out.println(address.getAddress().getHostAddress());
回答by aroth
myString = myString.replace("/", "");