提取 IP 地址最后一部分的最简单方法是什么?
我有一个 IP 地址,我想将其最后一块作为整数获取。因此,从 "192.168.1.150"
我会得到 150
。
这是我编写的代码(我使用的是 C++/CLI),但不知怎的,它感觉相当笨重:
String^ ipString = "192.168.1.150";
int lastDot = ipString->LastIndexOf('.');
int lastSection = int::Parse(ipString->Substring(lastDot, ipString->Length-lastDot));
有没有更简单的方法来做到这一点?
I have an IP address which I want to grab the last chunk of as an integer. So from "192.168.1.150"
I'd get 150
.
This is the code I'd concocted (I'm using C++/CLI), but somehow it feels rather clunky:
String^ ipString = "192.168.1.150";
int lastDot = ipString->LastIndexOf('.');
int lastSection = int::Parse(ipString->Substring(lastDot, ipString->Length-lastDot));
Is there a simpler way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码真的有效吗?难道它不应该读吗?:
如果您想要字符串中剩下的所有内容,则不必指定子字符串的长度,因此您可以将其缩短为:
虽然没有太大改进,但我怀疑您可以做得更好。
Does that code really work? Shouldn't it read?:
You don't have to specify the lengt to Substring if you want all that's left in the string, so you can shorten it to:
Not much of an improvement though, but I doubt you can do much better.