首页 > 代码库 > String .split完整测试

String .split完整测试

数据库中有一个字段是用逗号拼接的,每个字段都是另一个表中的主键,需要查询子元素,或者给这个逗号拼接的字段删减元素时,就会有比较复杂的字符串解析。

为此,先测试了String提供的split方法。

可以看到,元素之前的逗号都被认为是分隔符,劈开后进了字符串数组;

           而之后的逗号被抛弃。

        所以在做字段解析时,应该把重点放在“去除前面的逗号”,这样才能保证字符串数组的素值是那个表的主键,而不是空串。

@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*/            }


二、给老字符串里加入新元素

 

三、删掉老字符串里的某元素

String .split完整测试