Import Java.util.arrays;import org.junit.test;/** * Learn the string split method with a few examples * especially remember how to use multiple separators * @author LZG * */public clas s Stringsplittest {/** * The string to be delimited is all composed of delimiters */@Testpublic void Test1 () {string str = ",,,,,,"; String [] Strarray = Str.split (","); System.out.printf ("Length:%d \ n", strarray.length),//length:0 System.out.printf ("Content:%s", Arrays.tostring ( Strarray)//content: []}/** * If there are multiple delimiters, enclose in brackets */@Testpublic void Test2 () {String str = ",,:,;,"; String [] Strarray = Str.split ("[,;:]"); System.out.printf ("Length:%d \ n", strarray.length),//length:0 System.out.printf ("Content:%s", Arrays.tostring ( Strarray));//content: []}/** * The left side of the content is empty before encountering a non-delimited character. The right-most delimited content is ignored if empty */@Testpublic void Test3 () {String str = ", ab,cd,efgh,,,"; String [] Strarray = Str.split (","); System.out.printf ("Length:%d \ n", strarray.length); Length:4 System.out.printf ("Content:%s", arrays.tostring (Strarray));//content: [, AB, CD, efgh]}/** * before non-delimited characters are encountered on the left side, Empty content after separation is also preserved. The right-most delimited content is ignored if it is empty */@Testpublic void test4 () {Stringstr = ", ab,cd:efgh,;,"; String [] Strarray = Str.split ("[,;:]"); System.out.printf ("Length:%d \ n", strarray.length); length:4system.out.printf ("Content:%s", arrays.tostring (Strarray));//content: [, AB, CD, efgh]}/** * If the delimiter is a special character of the regular expression, it needs to be escaped/@Testpublic void Test5 () {String str = "*ab*cd*efgh***"; String [] Strarray = Str.split ("\\*"); System.out.printf ("Length:%d \ n", strarray.length),//length:4 System.out.printf ("Content:%s", Arrays.tostring ( Strarray));//content: [, AB, CD, efgh]}/** * delimiter is a special character of a regular expression, you need to escape. If the bracket itself is a delimiter, you need to escape */@Testpublic void Test6 () {String str = "*ab+cd^efgh[[["; String [] Strarray = Str.split ("[\\*\\+\\[^]"); System.out.printf ("Length:%d \ n", strarray.length),//length:4system.out.printf ("Content:%s", Arrays.tostring ( Strarray));//content: [, AB, CD, efgh]}/** * Multiple separators can be separated by | or not. If | is a delimiter, it needs to be escaped */@Testpublic void Test7 () {String str = "*ab+cd^efgh[[["; String [] Strarray = Str.split ("[\\*|\\+|\\[|^]"); System.out.printf ("Length:%d \ n", strarray.length); Length:4system.out.printf ("Content:%s", arrays.tostring (Strarray));//content: [, AB, CD, efgh]}/** * using standard regular */@Testpublic void Test8 () {String str = "*AB+CD^EFGH----234---[[...] ["; String [] Strarray = Str.split ("\\w+"); Note the meaning represented by the regular expression System.out.printf ("Length:%d \ n", strarray.length); Length:5 System.out.printf ("Content:%s", arrays.tostring (Strarray));//content: [, AB, CD, EFGH, 234]}/** * Split also has a restricted way of specifying the maximum number of strings to match */@Testpublic void Test9 () {String str = "*AB+CD^EFGH----234---[[...] ["; String [] Strarray = Str.split ("\\w+", 3); Limit matches up to 3 string System.out.printf ("Length:%d \ n", strarray.length); Length:3 System.out.printf ("Content:%s", arrays.tostring (Strarray));//content: [, AB, CD^EFGH----234---[[...] []}}
String split delimited string, multiple separators, regular expression learning examples