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?
String bbb[] = aaa.split("#");
String ccc = "## ";
String ddd[] = ccc.split("#");
System.out.println(bbb.length);//0
System.out.println(ddd.length);//3
why?
对于 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
--------------------------------------------------------------------------------
split 如果分割之后,如果在最后存在""(空串),默认是舍去的。比如"##",分割之后实际是"","","",可以从后面看,将""舍去,因此数组最后是0.
String ccc = "## ";
String ddd[] = ccc.split("#");
这种,因为最后存在一个空格,所以不存在这种舍去的情况。为了避免舍去,可以这样
String aaa = "##";
String bbb[] = aaa.split("#",-1);
如:字符串是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;
上面三种情况是我们常用见得。楼主的疑问我觉得是我们经常会犯得错误,“空格”=“不存在”
谢谢。
如:
字符串是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"]
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。在指定第二个参数大小的情况下,按参数指定的次数进行匹配,生成的所有空字符串都将被保留。
String aa="h11$hhj$k333$G111";
String[] a=aa.split("$");
System.out.println(a.length);
我以为 String ccc = "## "; 后面没空格. String aa="h11$hhj$k333$G111";
String[] a=aa.split("$");
System.out.println(a.length);应写为String [] a=aa.split("\\$");特殊字符要转译
请问你们公司搞 Java 的是不是英文都NB的不得了!
真要是那样,算你 NB, 不是那样就别来 ZB
出现了不知道多少次了
看了些高人的解释 还是不明所以
我承认我弱了但是确实没有啥好学习的
over