String. Split full test

Source: Internet
Author: User

A field in the database is spliced with commas (,). Each field is the primary key of another table. You need to query the child element or delete the element for the field spliced with commas, there will be more complex string parsing.

Therefore, the split method provided by string is tested first.

As you can see, all the commas before the element are considered as delimiters and are split into a string array;

Then the comma is discarded.

Therefore, when parsing fields, we should focus on "Remove the comma before" to ensure that the prime value of the string array is the primary key of the table, rather than an empty string.

@Test    public void testSplit() {        String str1 = "1,2,3";        String[] strArr1 = str1.split(",");        String str2 = "1,2,";        String[] strArr2 = str2.split(",");        String str3 = ",2,3";        String[] strArr3 = str3.split(",");        String str4 = ",";        String[] strArr4 = str4.split(",");        String str5 = ",,";        String[] strArr5 = str5.split(",");        String str6 = ",,6";        String[] strArr6 = str6.split(",");        String str7 = "7,,";        String[] strArr7 = str7.split(",");        String str8 = ",8,";        String[] strArr8 = str8.split(",");        System.out.println("str1="+str1);        for (int i = 0; i <strArr1.length; i++) {            System.out.println("strArr1["+i+"]="+strArr1[i]);        }        System.out.println("**************************************");                System.out.println("str2="+str2);        for (int i = 0; i <strArr2.length; i++) {            System.out.println("strArr2["+i+"]="+strArr2[i]);        }        System.out.println("**************************************");        System.out.println("str3="+str3);        for (int i = 0; i <strArr3.length; i++) {            System.out.println("strArr3["+i+"]="+strArr3[i]);        }        System.out.println("**************************************");        System.out.println("str4="+str4);        for (int i = 0; i <strArr4.length; i++) {            System.out.println("strArr4["+i+"]="+strArr4[i]);        }        System.out.println("**************************************");        System.out.println("str5="+str5);        for (int i = 0; i <strArr5.length; i++) {            System.out.println("strArr5["+i+"]="+strArr5[i]);        }        System.out.println("**************************************");        System.out.println("str6="+str6);        for (int i = 0; i <strArr6.length; i++) {            System.out.println("strArr6["+i+"]="+strArr6[i]);        }        System.out.println("**************************************");        System.out.println("str7="+str7);        for (int i = 0; i <strArr7.length; i++) {            System.out.println("strArr7["+i+"]="+strArr7[i]);        }        System.out.println("**************************************");        System.out.println("str8="+str8);        for (int i = 0; i <strArr8.length; i++) {            System.out.println("strArr8["+i+"]="+strArr8[i]);        }        /**str1=1,2,3strArr1[0]=1strArr1[1]=2strArr1[2]=3**************************************str2=1,2,strArr2[0]=1strArr2[1]=2**************************************str3=,2,3strArr3[0]=strArr3[1]=2strArr3[2]=3**************************************str4=,**************************************str5=,,**************************************str6=,,6strArr6[0]=strArr6[1]=strArr6[2]=6**************************************str7=7,,strArr7[0]=7**************************************str8=,8,strArr8[0]=strArr8[1]=8*/            }


2. Add new elements to the old string

 

3. delete an element from the old string

String. Split full test

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.