java InetAddress.getByName(host) 前的斜线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7361802/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:40:12  来源:igfitidea点击:

Slash before InetAddress.getByName(host)

javanetworking

提问by P R

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

如何删除输出中的斜线InetAddress.getbyName



UPDATE

更新

Thanks everyone, I just did it.

谢谢大家,我刚刚做到了。

One of the solutions is:

解决方案之一是:

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

回答by Mark Peters

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

如果您只需要 IP,请使用主机地址:

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 InetAddressas 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

您看到的斜杠可能是当您toString()InetAddress尝试打印时对返回的内容进行隐式操作时,它会打印由斜杠(例如stackoverflow.com/64.34.119.12)分隔的主机名和地址。你可以用

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 Stringintermediary here. InetAddresskeeps the two fields separate intrinsically.

但是完全没有理由去String这里找中介。 InetAddress保持两个领域本质上分开。

回答by Chris Aldrich

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

我假设您在此之后正在执行 toString ?为什么不使用普通的字符串操作,即子字符串?