String aaa = "##";
String bbb[] = aaa.split("#");
String ccc = "## ";
String ddd[] = ccc.split("#");
                System.out.println(bbb.length);//0
System.out.println(ddd.length);//3
why?

解决方案 »

  1.   

    java.lang.String.split(String regex) 以最后一个不为 "" 的字符串作为最后元素对于 aaa = "##" 来说,最后一个是 "" 所以 length = 0
    对于 bbb = "## " 来说,最后一个是 " " 所以 length = 3如果想将空字符串也记录,需要用 String[] java.lang.String.split(String regex, int limit)---------------------------------------------------------------------------------------
    String[] java.lang.String.split(String regex)split
    public String[] split(String regex)
    Splits this string around matches of the given regular expression. 
    This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result 
    : { "boo", "and", "foo" } 
    o { "b", "", ":and:f" } 
    Parameters:
    regex - the delimiting regular expression 
    Returns:
    the array of strings computed by splitting this string around matches of the given regular expression 
    Throws: 
    PatternSyntaxException - if the regular expression's syntax is invalid
    Since: 
    1.4 
    See Also:
    Pattern
    --------------------------------------------------------------------------------String[] java.lang.String.split(String regex, int limit)split
    public String[] split(String regex,
                          int limit)
    Splits this string around matches of the given regular expression. 
    The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string. The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. 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. The string "boo:and:foo", for example, yields the following results with these parameters: Regex Limit Result 
    : 2 { "boo", "and:foo" } 
    : 5 { "boo", "and", "foo" } 
    : -2 { "boo", "and", "foo" } 
    o 5 { "b", "", ":and:f", "", "" } 
    o -2 { "b", "", ":and:f", "", "" } 
    o 0 { "b", "", ":and:f" } An invocation of this method of the form str.split(regex, n) yields the same result as the expression Pattern.compile(regex).split(str, n) Parameters:
    regex - the delimiting regular expression
    limit - the result threshold, as described above 
    Returns:
    the array of strings computed by splitting this string around matches of the given regular expression 
    Throws: 
    PatternSyntaxException - if the regular expression's syntax is invalid
    Since: 
    1.4 
    See Also:
    Pattern
    --------------------------------------------------------------------------------
      

  2.   

    CSDN 的老题目了
    split 如果分割之后,如果在最后存在""(空串),默认是舍去的。比如"##",分割之后实际是"","","",可以从后面看,将""舍去,因此数组最后是0.
    String ccc = "## "; 
    String ddd[] = ccc.split("#"); 
    这种,因为最后存在一个空格,所以不存在这种舍去的情况。为了避免舍去,可以这样
    String aaa = "##";
    String bbb[] = aaa.split("#",-1);
      

  3.   

    关键就在那个空格。把楼主的例子中的"#"换成",",把空格换成"2",这样看更容易理解。相信看完下面就明白是怎么回事了。
                       如:字符串是str=",,,,";strA[]=str.split(",");str.length=0;
                          字符串是str="2,,,,";strA[]=str.split(",");str.length=1;
                       字符串是str=",2,,,";strA[]=str.split(",");str.length=2;
                       字符串是str=",,2,,";strA[]=str.split(",");str.length=3;
                       字符串是str=",,,4,";strA[]=str.split(",");str.length=4;
                        
                       上面三种情况是我们常用见得。楼主的疑问我觉得是我们经常会犯得错误,“空格”=“不存在”
      

  4.   

    更多关于JAVA开源项目,JAVA算法,JS开源项目,WEB AJAX应用,请到126Code开源时代,让我们一起将开源进行到底.
      

  5.   

    java初学者  学习过了。。
      

  6.   

    要拿这些来 分析的话,那肯定很多我们都不知道了,因为 现在很少人会去 专研 每一个API。不过 这样的分享 也让更多的人 了解了更多的API了。
    谢谢。
      

  7.   

    字符串处理的时候一般都trim()下,split和转数字前都写正则验证一下.编程要有好习惯原来被properties里面的空格搞死过,唉
      

  8.   

    java.lang.String.split(String regex) 以最后一个不为 "" 的字符串作为最后元素
      

  9.   

    java.lang.String.split(String regex) 以最后一个不为 "" 的字符串作为最后元素 
    如: 
    字符串是str=",,,,";strA[]=str.split(",");str.length=0; //[""][""][""][""][""]=> 
    字符串是str="2,,,,";strA[]=str.split(",");str.length=1;//["2"][""][""][""][""]=>["2"] 
    字符串是str=",2,,,";strA[]=str.split(",");str.length=2;//[""]["2"][""][""][""]=>[""]["2"] 
    字符串是str=",,2,,";strA[]=str.split(",");str.length=3;//[""][""]["2"][""][""]=>[""][""]["2"] 
    字符串是str=",,,4,";strA[]=str.split(",");str.length=4;//[""][""][""]["2"][""]=>[""][""][""]["2"] 
     
      

  10.   

    public class Main {
    String ppp[] = {"","",""};
    String bbb[] = "##".split("#",3);
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Main m = new Main();
    System.out.println("m.ppp is "+m.ppp.length+"\n"
    +"m.bbb is "+m.bbb.length
    );
    } /* (non-Java-doc)
     * @see java.lang.Object#Object()
     */
    public Main() {
    super();
    }}
    --------------
    Result:m.ppp is 3
    m.bbb is 3
    --------------
    Analysis:1。没有指定第二个参数的大小的情况下,拆分的字符串数组中,最后一个不是空字符串的元素是生成的数组的最后一个元素。P.S. 这个元素后面的空字符串元素将被删除。
    2。在指定第二个参数大小的情况下,按参数指定的次数进行匹配,生成的所有空字符串都将被保留。
      

  11.   

    还有一个容易犯错的地方,就是在String.split()中的分割字符时一个正则表达式的特殊字符,那样就分割不出来了。如:
                      String aa="h11$hhj$k333$G111";
    String[] a=aa.split("$");
    System.out.println(a.length);
      

  12.   

    我晕
    我以为 String ccc = "## "; 后面没空格.  String aa="h11$hhj$k333$G111"; 
    String[] a=aa.split("$"); 
    System.out.println(a.length);应写为String [] a=aa.split("\\$");特殊字符要转译
      

  13.   


    请问你们公司搞 Java 的是不是英文都NB的不得了!
    真要是那样,算你 NB, 不是那样就别来 ZB
      

  14.   

    哗众取宠的老题目
    出现了不知道多少次了
    看了些高人的解释 还是不明所以
    我承认我弱了但是确实没有啥好学习的
    over