7

我正在准备 OCPJP 考试,我遇到了以下示例:

class Test {
   public static void main(String args[]) {
      String test = "I am preparing for OCPJP";
      String[] tokens = test.split("\\S");
      System.out.println(tokens.length);
   }
}

此代码打印 16。我期待类似 no_of_characters + 1 的东西。有人可以解释一下,在这种情况下 split() 方法实际上做了什么?我只是不明白...

4

1 回答 1

14

它拆分"\\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 个元素。

于 2014-03-07T20:17:25.923 回答