在InetAddress.getByName(host)之前斜杠

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


UPDATE

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

其中一个解决方案是:

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

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

 String address = InetAddress.getByName("stackoverflow.com").getHostAddress(); 

如果您只想要主机名,请使用

 String hostname = InetAddress.getByName("stackoverflow.com").getHostName(); 

编辑

您看到的斜杠可能是在您尝试将其打印出来时对返回的InetAddress执行隐式toString() ,这会打印由斜杠分隔的主机名和地址(例如stackoverflow.com/64.34.119.12 )。 你可以用

 String address = InetAddress.getByName("stackoverflow.com").toString().split("/")[1]; String hostname = InetAddress.getByName("stackoverflow.com").toString().split("/")[0]; 

但是根本没有理由去这里的String中介。 InetAddress本质上保持两个字段分开。

我假设你在这之后做了一个toString? 你为什么不使用普通的字符串操作,意思是子字符串?