String.split的陷阱

1.      部分特殊字符不能直接分割,如

String[] array ="www.battlenet.com.cn".split(".");
按”.”分割,但出来的结果,array.length为0,数组里面什么也没有

2.      结尾连续多个分割符号,分割结果丢失,如

String[] array ="a,,b,c,,,".split(",");
结果数组长度4,分别为”a”,””,”b”,”c”,其后无分割结果

 

搞清楚这两个陷阱,其实查一下JDK的API就好了(其实也是告诉我们文档的重要性和读文档的重要性),split方法API如下(JKD1.7版本):

public String[] split(String regex)

 

Splits this string around matches of thegiven regular expression.

 

This method works as if by invoking thetwo-argument split method with the given expression and a limit argument ofzero. Trailing empty strings are therefore not included in the resulting array.

 

The string "boo:and:foo", forexample, 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 ofthe given regular expression

Throws:

   PatternSyntaxException - if the regular expression's syntax is invalid

Since:

   1.4

See Also:

Pattern

 

通读一遍这个描述,我们可以得出结论:

1.      参数regex为“划分用的正则表达式”,而我们知道’.’在Java正则表达式中是任意字符的通配,并非真正的小数点的意思,所以这个需要转义为\.’,写作

String[] array ="www.battlenet.com.cn".split("\\.");    //注意’\’本身也是需要转义的

同样值得注意的还有各种括号,^,$,*等在正则表达式中有特殊意义的符号

2.      “This method works as if byinvoking the two-argument split method with the given expression and a limitargument of zero”.这里提到了双参数的split方法,我们继续去看双参数的split方法的API(我这里就不贴出来占版面了),发现第二个参数int limit是分割结果阀值,意思就是最多划分多少次,并且在描述里面有如下说明:

“……If n is non-positive then thepattern will be applied as many times as possible and the array can have anylength. If n is zero then the pattern will be applied as many times aspossible, the array can have any length, and trailing empty strings will bediscarded.”

文档清楚说明了当limit为0是,空串会被丢弃,结合API中对” boo:and:foo”划分’o’(对双参数方法参数即为(‘o’,0))的结果为{ "b", "", ":and:f" },可以理解为对末尾空串会丢弃,而对中间的空串则不会丢弃。想不被丢弃末尾空串,因此只需为双参数方法中limit赋值负数即可(这点在API中也已给出例子说明),例如:

String[] array ="a,,b,c,,,".split(",", -1);
 

以上留给自己记录,也留给各位初学的朋友和与我一样曾经踩过这两个陷阱的童鞋们。


2015-08-14

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值