问题是这样的!
String s="1234[代码]~32434~32424~123123~~~~~~";
String x[]=s.split("~");
int size=x.length;
System.out.println(size);
结果size的长度是4,而不是9。我现在搞不明白为什么~~之间就不能解析!我想~~之间应该是空值!
为什么分离时数组的长度是4而不是9!我要得到9,而且后面5个是空值!
求大家帮我分析一下!

解决方案 »

  1.   

    有两种方法得到你的结果:
    1): String x[]=s.split("~");
       改为:String x[]=s.split("~",10); (大于9就好)
    2): String s="1234[代码]~32434~32424~123123~~~~~~";
        在最后加任意字符 如  String s="1234[代码]~32434~32424~123123~~~~~~0";在JDK  文档里有说明
    If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded. 
      

  2.   

    哦对了 刚才的结果应该是 10 而不是9
    split(String reg) 是调用 split(String reg,int limit)
    既是:split(reg,0);
    对于了limit = 0 时 将丢弃尾部的空字符串 既为 4.