简体   繁体   English

Java - 为什么字符串拆分为空字符串会给我一个非空数组?

[英]Java - Why does string split for empty string give me a non empty array?

I want to split a String by a space.我想用一个空格分割一个字符串。 When I use an empty string, I expect to get an array of zero strings.当我使用空字符串时,我希望得到一个零字符串数组。 Instead, I get an array with only empty string.相反,我得到一个只有空字符串的数组。 Why ?为什么 ?

public static void main(String [] args){
    String x = "";
    String [] xs = x.split(" ");
    System.out.println("strings :" + xs.length);//prints 1 instead of 0.
}

The single element string array entry is in fact empty string.单元素字符串数组条目实际上是空字符串。 This makes sense, because the split on " " fails, and hence you just get back the input with which you started.这是有道理的,因为对" "的拆分失败了,因此您只需取回开始时的输入。 As a general approach, you may consider that if splitting returns you a single element, then the split did not match anything, leaving you with the starting input string.作为一般方法,您可能会考虑,如果拆分返回单个元素,则拆分不匹配任何内容,为您留下起始输入字符串。

An interesting puzzle indeed:确实是一个有趣的谜题:

> "".split(" ")
String[1] { "" }
> " ".split(" ")
String[0] {  }

The question is, when you split the empty string, why does the result contain the empty string, and when you split a space, why does the result not contain anything?现在的问题是,当你拆空字符串,为什么结果包含空字符串,而当你分割一个空间,为什么结果包含任何内容? It seems inconsistent, but all is explained in the documentation.这似乎不一致,但所有内容都在文档中进行了解释。

The String.split(String) method "works as if by invoking the two-argument split method with the given expression and a limit argument of zero" , so let's read the docs for String.split(String, int) . String.split(String)方法“就像通过使用给定的表达式和零限制参数调用双参数 split 方法一样工作” ,所以让我们阅读String.split(String, int)的文档。 The case of the empty string is answered by this part:空字符串的情况由这部分回答:

If the expression does not match any part of the input then the resulting array has just one element, namely this string.如果表达式不匹配输入的任何部分,则结果数组只有一个元素,即这个字符串。

The empty string has no part matching a space, so the output is an array containing one element, the input string, exactly as the docs say should happen.空字符串没有与空格匹配的部分,因此输出是一个包含一个元素的数组,即输入字符串,正如文档所说应该发生的那样。

The case of the string " " is answered by these two parts:字符串" "情况由这两部分回答:

A zero-width match at the beginning however never produces such empty leading substring.然而,开头的零宽度匹配永远不会产生这样的空前导子串。

If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.如果n为零,则该模式将被应用尽可能多的次数,数组可以具有任意长度,并且将丢弃尾随的空字符串。

The whole input string " " matches the splitting pattern.整个输入字符串" "与拆分模式匹配。 In principle we could include an empty string on either side of the match, but the docs say that an empty leading substring is never included, and (because the limit parameter n = 0) the trailing empty string is also discarded.原则上我们可以在匹配的任一侧包含一个空字符串,但文档说从不包含空的前导子字符串,并且(因为限制参数n = 0)尾随空字符串也被丢弃。 Hence, the empty strings before and after the match are both not included in the resulting array, so it's empty.因此,匹配前后的空字符串都不包含在结果数组中,因此它是空的。

It appears that since the String exists and it cannot be split (there are no spaces), it simply places the entire String into the first array position, causing there to be one.看起来,由于 String 存在且无法拆分(没有空格),它只是将整个 String 放入第一个数组位置,从而导致有一个。 If you were to instead try如果你要尝试

String x = " ";
String [] xs = x.split(" ");
System.out.println("strings :" + xs.length);//prints 1 instead of 0.

It will give you the zero you are expecting.它会给你你期望的零。

See also: Java String split removed empty values另请参阅: Java String split 删除了空值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM