java中split分割"."的問題

今天使用split分割"."的時候竟然失敗了,通過百度發現原來要加轉義字符才行。express

正確的寫法:數組

String test="1.2.3";
String[] s1=test.split("\\.");

結果:函數

若是不加轉義就會分割失敗,返回一個空的字符串數組。this

API中是這樣寫的:spa

public String[] split(String regex)code

 Splits this string around matches of the given regular expression. blog

它的參數並非要分割的字符串,而是一個表達式,所以像*、+等都要加轉義字符串

可是使用substring函數就沒有這個問題string

例如取文件名的後綴能夠這樣寫:it

String fileSuffix=fileName.substring(fileName.lastIndexOf(".")+1);
相關文章
相關標籤/搜索