开发者

What's the simplest way to extract the last section of an IP address?

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 = in开发者_如何学运维t::Parse(ipString->Substring(lastDot, ipString->Length-lastDot));

Is there a simpler way of doing this?


Does that code really work? Shouldn't it read?:

int lastDot = ipString->LastIndexOf('.') + 1;

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:

String^ ipString = "192.168.1.150";  
int lastSection = int::Parse(ipString->Substring(ipString->LastIndexOf('.') + 1) ; 

Not much of an improvement though, but I doubt you can do much better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜