regex会切割掉,不会包含在string[]子穿中
API:
public String[] split(String regex)
public String[] split(String regex, int limit)
regex:切割正则匹配
limit:
当limit>0时,切割次数=limit-1,因此当limit=1时,不切割,原字符串输出
当limit=0与不写limit一致:不限制切割次数,出现几次匹配切割几次。但是去除最后空字符串
当limit<0,不限制切割次数,出现几次匹配切割几次,不去除最后空字符串。
eg:
String s3 = "pwwkew";
String[] as1 = s3.split("w",1);//{"pwwkew"}
String[] as2 = s3.split("w",2);//{"p","wkew"}
String[] as3 = s3.split("w",3);//{"p","","kew"}
String[] as_ = s3.split("w");//{"p","","ke"}
String[] as0 = s3.split("w",0);//{"p","","ke"}
String[] as01 = s3.split("w",-1);//{"p","","ke",""}
总结经常使用场景:
0或者不带limit:用在获取切割后的片段
limit=-1:根据String[].length-1=切割次数