问题描述
今天发生在我的java的行为 String.split()
是很奇怪的。
It occured to me today the behavior of java String.split()
is very strange.
其实我想要分割字符串AA,BB,CC,DD ,,, EE
来阵列由 .split( )
这给了我一个String数组 [AA,BB,抄送,DD,,,EE]
长7。
Actually I want to split a string "aa,bb,cc,dd,,,ee"
to array by .split(",")
that gives me a String array ["aa","bb","cc","dd","","","ee"]
of length 7.
但是当我尝试拆分字符串AA,BB,CC,DD ,,,,
来排列这给了我一个长度为4的数组只是意味着 [AA,BB,抄送,DD]
拒绝所有一个空白字符串。
But when I try to split a String "aa,bb,cc,dd,,,,"
to array this gives me a array of length 4 means only ["aa","bb","cc","dd"]
rejecting all next blank Strings.
我要那个分裂像AA,BB,CC,DD ,,,,
来阵列 [AA的字符串的程序,BB,抄送,DD,,,]
。
I want a procedure that splits a String like "aa,bb,cc,dd,,,,"
to array ["aa","bb","cc","dd","","",""]
.
这可能与java.lang.String中的API?先谢谢了。
Is this possible with java.lang.String api? Thanks in advance.
推荐答案
使用String.split(String正则表达式,INT限制) 负限制(例如-1)。
Use String.split(String regex, int limit)
with negative limit (e.g. -1).
"aa,bb,cc,dd,,,,".split(",", -1)
在String.split(String正则表达式) 被调用时,它被称为与限制
= 0,这将删除阵列中的所有尾随空字符串(在大多数情况下,见下文)。
When String.split(String regex)
is called, it is called with limit
= 0, which will remove all trailing empty strings in the array (in most cases, see below).
的实际行为 String.split(字符串正则表达式)
还是比较混乱的:
The actual behavior of String.split(String regex)
is quite confusing:
,
或;;;
与正则表达式
是,
将导致空数组。的非空字符串分割将导致删除阵列中的所有尾随空字符串的
Splitting an empty string will result in an array of length 1. Empty string split will always result in length 1 array containing the empty string.
Splitting ";"
or ";;;"
with regex
being ";"
will result in an empty array. Non-empty string split will result in all trailing empty strings in the array removed.
以上可以观察到至少5的Java到Java 8。行为
The behavior above can be observed from at least Java 5 to Java 8.
有是企图去改变在拆分 JDK-6559590 。然而,它很快就在 JDK-8028321 恢复时,它会导致回归在不同的地方。这种变化从不入初始Java 8版本。
There was an attempt to change the behavior to return an empty array when splitting an empty string in JDK-6559590. However, it was soon reverted in JDK-8028321 when it causes regression in various places. The change never makes it into the initial Java 8 release.