开发者

Getting a part of IP address String

If I have a IP address String like : 10.120.230.78 I would like to get 10.120. out of it

But the parts of the address can change from 1 tot 3 numbers as we all know ...

1.1.1.1 to 255.255.255.255 so ....

I believe you can use a pattern, but I don't have an idea how to.开发者_JAVA百科

Please, any suggestions would be welcome.

thx all


assume you store your ip as a string called ip, you can use String.split() to get an array of the parts:

String[] tokens = ip.split("\\.");


ip.substring(0, ip.indexOf('.',ip.indexOf('.') + 1) + 1)

where ip is the string holding the IP address.


1) The best way is String.split() as already mentioned by amit. E.g. String[] ipAddressParts = ipAddress.split();

2) Also you can use StringTokenizer stringTokenizer = new StringTokenizer( ipAddress, "." ); while ( stringTokenizer.hasMoreTokens() ) { System.out.println( stringTokenizer.nextToken() ); }

3) java.util.Scanner


Take a look at String.split() it's equivalent to php's explode()

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜