本帖最后由 赵永康 于 2012-9-23 00:41 编辑
大家看看一下程序:
import java.util.*;
public class S{
public static void main(String[] args){
String s = "::boo:and:foo::";
System.out.println(Arrays.toString(s.split(":",-4)));
}
}
现在的结果是" "," ","boo","and","foo"," "," "
1、假如我把s=":boo:and:foo:"; 结果就会是:" ","boo","and","foo"," "
2、假如我把s="boo:and:foo"; 结果就会是:"boo","and","foo"
3、假如我把s="boo::and::foo"::; 结果就会是:"boo","," ",and","foo"," "," "
由此我得出的结论就是:
对于在切割的时候:根据满足条件的正则的位置不同而不同。具体就是
假如s中有N个连续满足正则条件的
假设满足正则出现在s的开头或者结尾处:那么切割完后的字符串数组就会保留其N个位置,并给其值为" ";
假如满足正则的出现的s之内,那么切割完后的字符串数组就会保留期N-1个位置,并给其值为" "。
和大家分享一下,以后再使用正则的时候,注意一下哈啊!
|