2011-12-28 401 views

回答

5

java.lang.String.split();
用法: 此方法返回的數組包含此字符串的每個子字符串,該字符串由與給定表達式匹配的另一個子字符串終止,或者以字符串結尾終止。數組中的子字符串按照它們在此字符串中出現的順序排列。如果表達式不匹配輸入的任何部分,則結果數組只有一個元素,即該字符串。

org.apache.commons.lang.StringUtils.splitPreserveAllTokens();
用法: 拆分提供的文本到一個數組,指定的分隔符,保留所有的令牌,包括由相鄰分隔件創建的空令牌。這是使用StringTokenizer的替代方法。

瞭解更多:kickjava_src_apache_StringUtils

String.split()使用final類Pattern分裂。

Pattern.compile(regex).split(this , limit); 
在StringUtils的

使用splitWorker(String str, char separatorChar, boolean preserveAllTokens),它自己的方法,其是用於2.0(JDK1.4)演奏曲調。

+0

是不是包含空的標記? – 2011-12-28 12:18:52

+0

你可以從這個[問題]找到確切的答案(http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters) – 2011-12-28 12:37:19

+0

謝謝阿扎德。我使用逗號作爲分隔符,所以我想這裏不應該與String.split()有任何問題。但是我會失去由StringUtils splitworker提供的性能改進,我必須自己處理null。 – 2011-12-28 13:06:31

2
I found folowing difference between String.split and splitByWholeSeparatorPreserveAllTokens 
  • splitByWholeSeparatorPreserveAllTokens處理其中 String.split()不
  • 在splitByWholeSeparatorPreserveAllTokensAdjacent隔板 當作隔板空令牌Null值。