String.split returning null when using a dot

后端 未结 3 1400
忘掉有多难
忘掉有多难 2020-12-06 09:41

I got this simple code:

String ip = \"1.2.3.4\";
String[] ipArray = ip.split(\".\");
System.out.println(ipArray[1]);

And ipArray

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 10:09

    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.

提交回复
热议问题