Java split

String split 用得太多了,但是今天编代码时发现好几个同事犯错了! 好奇心来了,顺便测试了群里一帮人,90%出错了,呵呵! 有兴趣你自己也测试一下吧! 答案在文后!

1. 

String st3 = "";

System.out.println(st3.split("\\s+").length);

打印多少呢?


2. 

String st2 = " hi, Marty";

System.out.println(st2.split("\\s+").length);


2. 

String st4 = "   ";

System.out.println(st4.split("\\s+").length);


大家参考一下文档或者文章http://shukuiyan.iteye.com/blog/1058672

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:

RegexResult
:{ "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


答案: 1, 3, 0



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值