I got this simple code:
我有一个简单的代码:
String ip = "1.2.3.4";
String[] ipArray = ip.split(".");
System.out.println(ipArray[1]);
And ipArray
is null by the time it hits System.out.println
(throws null pointer exception).
当ipArray到达System.out时,它是空的。println(抛出空指针异常)。
My question is why does ipArray stay null even though I'm setting it to split on each of ip's .s?
我的问题是为什么ipArray保持为空即使我将它设置为在每个ip的。s上分裂?
3 个解决方案
#1
44
Use ip.split("\\.");
and your problems will be solved. The problem is that String#split receives a regular expression, the dot (.) symbol is a special character in regular expressions, so you need to escape it for it to be interpreted as a plain dot, also as the backslash is an escape character in Java, you have to escape it too.
使用ip.split(“\ \”。);你的问题会得到解决。问题是字符串#分裂收到一个正则表达式,点(.)符号是正则表达式的特殊字符,所以你需要逃离它被解释为一个普通点,也是反斜杠是一个转义字符在Java中,你也必须逃避它。
#2
41
.
is a special character in regular expressions, which is what is used to split a string.
。是正则表达式中的一个特殊字符,用于分割字符串。
To get around this, you need to escape the .
. That leads us to \.
Unfortunately, \
is ALSO a special character in the java string, so that must also be escaped, to make \\.
为了解决这个问题,你需要逃离…这就引出了\。不幸的是,\也是java字符串中的一个特殊字符,因此必须对其进行转义,以生成\。
Our "final" result is ip.split("\\.");
我们的最终结果是ip.split("\ . \.");
In a related issue, the whole process can be averted entirely. There's no sense in doing something that a standard library already has done for us.
在一个相关的问题上,整个过程可以完全避免。做一些标准库已经为我们做过的事情是没有意义的。
Consider the following
考虑以下
byte[] ipOctets = InetAddress.getByName(ip).getAddress();
The only issue here is to remember that if you want the int value, you have to extract it with &
like int octet = ipOctets[0] & 0xFF;
这里唯一的问题是要记住,如果你想要int值,你必须使用& like int octet = ipOctets[0] & 0xFF;
#3
5
Pattern.quote(String) can also be used to quote the whole string. This returns a pattern which will be interpreted literally, special characters will have no special meaning. This might be overkill in this case, but sometimes it can be useful.
引用(String)也可以用来引用整个字符串。这将返回一个将被字面解释的模式,特殊字符将没有特殊含义。在这种情况下,这可能有点过头了,但有时它也很有用。
#1
44
Use ip.split("\\.");
and your problems will be solved. The problem is that String#split receives a regular expression, the dot (.) symbol is a special character in regular expressions, so you need to escape it for it to be interpreted as a plain dot, also as the backslash is an escape character in Java, you have to escape it too.
使用ip.split(“\ \”。);你的问题会得到解决。问题是字符串#分裂收到一个正则表达式,点(.)符号是正则表达式的特殊字符,所以你需要逃离它被解释为一个普通点,也是反斜杠是一个转义字符在Java中,你也必须逃避它。
#2
41
.
is a special character in regular expressions, which is what is used to split a string.
。是正则表达式中的一个特殊字符,用于分割字符串。
To get around this, you need to escape the .
. That leads us to \.
Unfortunately, \
is ALSO a special character in the java string, so that must also be escaped, to make \\.
为了解决这个问题,你需要逃离…这就引出了\。不幸的是,\也是java字符串中的一个特殊字符,因此必须对其进行转义,以生成\。
Our "final" result is ip.split("\\.");
我们的最终结果是ip.split("\ . \.");
In a related issue, the whole process can be averted entirely. There's no sense in doing something that a standard library already has done for us.
在一个相关的问题上,整个过程可以完全避免。做一些标准库已经为我们做过的事情是没有意义的。
Consider the following
考虑以下
byte[] ipOctets = InetAddress.getByName(ip).getAddress();
The only issue here is to remember that if you want the int value, you have to extract it with &
like int octet = ipOctets[0] & 0xFF;
这里唯一的问题是要记住,如果你想要int值,你必须使用& like int octet = ipOctets[0] & 0xFF;
#3
5
Pattern.quote(String) can also be used to quote the whole string. This returns a pattern which will be interpreted literally, special characters will have no special meaning. This might be overkill in this case, but sometimes it can be useful.
引用(String)也可以用来引用整个字符串。这将返回一个将被字面解释的模式,特殊字符将没有特殊含义。在这种情况下,这可能有点过头了,但有时它也很有用。