最近寫了個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("\\.");