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:

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


答案: 1, 3, 0



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章