String split delimited string, multiple separators, regular expression learning examples

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.