最近写了个Java split()函数 参数为".",竟然没有结果,以下html
public static void main(String[] args){ String ip="192.168.1.111"; String[] temp=ip.split("."); System.out.println(temp.length); }
返回的temp的长度为0,经过查阅,才知道split的参数是String regex 表明正则表达式,若是里面包含特殊字符,就不能用了,"."正好是一个特殊字符。java
两种方法能够解决正则表达式
String[] temp=ip.split("[.]");
或函数
String[] temp=ip.split("\\.");