1、參考文獻java
1.1 https://blog.csdn.net/daxiang_zhang/article/details/2149896正則表達式
1.2 java String.split()使用注意點和問題express
2、用法(徹底copy的參考文獻1.1,用做備份,侵刪)數組
java.lang.string.split
split 方法
將一個字符串分割爲子字符串,而後將結果做爲字符串數組返回。
stringObj.split([separator,[limit]])
stringObj
必選項。要被分解的 String 對象或文字。該對象不會被 split 方法修改。
separator
可選項。字符串或 正則表達式 對象,它標識了分隔字符串時使用的是一個仍是多個字符。若是忽
略該選項,返回包含整個字符串的單一元素數組。
limit
可選項。該值用來限制返回數組中的元素個數。
說明:
split 方法的結果是一個字符串數組,在 stingObj 中每一個出現 separator 的位置都要進行分解
。separator 不做爲任何數組元素的部分返回。
示例1:this
public class SplitDemo { public static String[] ss = new String[20]; public SplitDemo() { String s = "The rain in Spain falls mainly in the plain."; // 在每一個空格字符處進行分解。 ss = s.split(" "); } public static void main(String[] args) { SplitDemo demo = new SplitDemo(); for (int i = 0; i < ss.length; i++) System.out.println(ss[i]); } } 程序結果: The rain in Spain falls mainly in the plain.
示例2:.net
public class SplitDemo { public static String[] ss = new String[20]; public SplitDemo() { String s = "The rain in Spain falls mainly in the plain."; // 在每一個空格字符處進行分解。 ss = s.split(" ", 2); } public static void main(String[] args) { SplitDemo demo = new SplitDemo(); for (int i = 0; i < ss.length; i++) System.out.println(ss[i]); } } 程序結果: The rain in Spain falls mainly in the plain.
示例3:code
public class SplitDemo { public static String[] ss = new String[20]; public SplitDemo() { String s = "The rain in Spain falls mainly in the plain."; // 在每一個空格字符處進行分解。 ss = s.split(" ", 20); } public static void main(String[] args) { SplitDemo demo = new SplitDemo(); for (int i = 0; i < ss.length; i++) System.out.println(ss[i]); } } 程序結果: The rain in Spain falls mainly in the plain.
示例4:對象
public class SplitDemo { public static void main(String[] args) { String value = "192.168.128.33"; String[] names = value.split("."); for (int i = 0; i < names.length; i++) { System.out.println(names[i]); } } } 運行結果: 對,沒看錯!沒有任何輸出! 讓咱們來看看 split 方法的方法簽名吧: public string[] split(string regex) 這裏的參數的名稱是 regex ,也就是 regular expression (正則表達式)。這個參數並非一個簡單的分割用的字符,而是一個正則表達式,看了 split 方法的實現代碼就更堅決了咱們的信心: public string[] split(string regex, int limit) { return pattern.compile(regex).split(this, limit); } split 的實現直接調用的 matcher 類的 split 的方法。讀者已經知道,「 . 」在正則表達式中有特殊的含義,所以咱們使用的時候必須進行轉義。 只要將 String[] names = value.split("."); 改成 String[] names = value.split("//."); 就能夠了。 輸出結果: 192 168 128 33
再加一點兒補充(這是Java幫助文檔中的,更清晰一些):
public String[] split(String regex,int limit)根據匹配給定的正則表達式來拆分此字符串。
此方法返回的數組包含此字符串的每一個子字符串,這些子字符串由另外一個匹配給定的表達式的子字符串終止或由字符串結束來終止。數組中的子字符串按它們在此字符串中的順序排列。若是表達式不匹配輸入的任何部分,則結果數組只具備一個元素,即此字符串。
limit 參數控制模式應用的次數,所以影響結果數組的長度。若是該限制 n 大於 0,則模式將被最多應用 n - 1 次,數組的長度將不會大於 n,並且數組的最後項將包含超出最後匹配的定界符的全部輸入。若是 n 爲非正,則模式將被應用盡量多的次數,並且數組能夠是任意長度。若是 n 爲零,則模式將被應用盡量多的次數,數組可有任何長度,而且結尾空字符串將被丟棄。
blog
例如,字符串 "boo:and:foo" 使用這些參數可生成下列結果: Regex Limit 結果 : 2 { "boo", "and:foo" } : 5 { "boo", "and", "foo" } : -2 { "boo", "and", "foo" } o 5 { "b", "", ":and:f", "", "" } o -2 { "b", "", ":and:f", "", "" } o 0 { "b", "", ":and:f" }
這種形式的方法調用 str.split(regex, n) 產生與如下表達式徹底相同的結果:
Pattern.compile(regex).split(str, n)
參數:
regex - 定界正則表達式
limit - 結果閾值,如上所述
返回:
字符串數組,根據給定正則表達式的匹配來拆分此字符串,從而生成此數組
拋出:
PatternSyntaxException - 若是正則表達式的語法無效
從如下版本開始:
1.4
public String[] split(String regex)根據給定的正則表達式的匹配來拆分此字符串。
該方法的做用就像是使用給定的表達式和限制參數 0 來調用兩參數 split 方法。所以,結果數組中不包括結尾空字符串。 文檔
例如,字符串 "boo:and:foo" 產生帶有下面這些表達式的結果: Regex 結果 : { "boo", "and", "foo" } o { "b", "", ":and:f" } 參數: regex - 定界正則表達式 返回: 字符串數組,根據給定正則表達式的匹配來拆分此字符串,從而生成此數組。 拋出: PatternSyntaxException - 若是正則表達式的語法無效