简体   繁体   English

操纵IP地址 - 在'。'上拆分字符串。 字符

[英]Manipulating IP addresses - split string on '.' character

String address = "192.168.1.1";

I want to split the address and the delimiter is the point. 我想分割地址和分隔符是重点。 So I used this code: 所以我使用了这段代码:

String [] split = address.split(".");

But it didn't work, when I used this code it works: 但它不起作用,当我使用这个代码它工作:

String [] split = address.split("\\.");

so why splitting the dot in IPv4 address is done like this : ("\\\\.") ? 那么为什么在IPv4地址中分割点是这样的:( ("\\\\.")

You need to escape the "." 你需要逃避“。” as split takes a regex. split需要一个正则表达式。 But you also need to escape the escape as "\\." 但你也需要逃脱“\\”。 won't work in a java String : 将无法在java String

String [] split = address.split("\\.");

This is because the backslash in a java String denotes the beginning of a character literal . 这是因为java String的反斜杠表示字符文字的开头。

You should split like this, small tip use Pattern.compile as well 你应该像这样拆分,小技巧也使用Pattern.compile

String address = "192.168.1.1";
String[] split = address.split("\\.");// you can replace it with private static final Pattern.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM