它拆分"\\S"
正则表达式引擎中代表\S
非空白字符的每个字符。
因此,让我们尝试"x x"
在非空白 ( \S
) 上进行拆分。由于这个正则表达式可以被一个字符匹配,让我们遍历它们以标记拆分位置(我们将使用管道|
)。
- 是非
'x'
空格吗?是的,所以让我们标记它| x
- 是非
' '
空格吗?不,所以我们保持原样
- 最后一个
'x'
非空格?是的,所以让我们标记它| |
因此,我们需要在开始和结束时拆分我们的字符串,这最初给了我们结果数组
["", " ", ""]
^ ^ - here we split
但是由于删除了尾随的空字符串,结果将是
[""," "] <- result
,""] <- removed trailing empty string
所以 split 返回["", " "]
仅包含两个元素的数组。
顺便提一句。要关闭删除最后一个空字符串,您需要使用split(regex,limit)
负值限制,例如split("\\S",-1)
.
现在让我们回到你的例子。对于您的数据,您将在每个数据上进行拆分
I am preparing for OCPJP
| || ||||||||| ||| |||||
意思是
""|" "|""|" "|""|""|""|""|""|""|""|""|" "|""|""|" "|""|""|""|""|""
所以这代表这个数组
[""," ",""," ","","","","","","","",""," ","",""," ","","","","",""]
但是由于""
删除了尾随的空字符串(如果它们的存在是由拆分引起的-更多信息请参见:来自 String.split 的混淆输出)
[""," ",""," ","","","","","","","",""," ","",""," ","","","","",""]
^^ ^^ ^^ ^^ ^^
你得到的结果数组只包含这部分:
[""," ",""," ","","","","","","","",""," ","",""," "]
正好是 16 个元素。