简体   繁体   English

没有字符的Java字符串(“”)以逗号分隔将返回数组大小1

[英]Java String with no characters(“”) Split with comma returns array size 1

I have a String with ""(blank data), whenever I try to split the string with comma, I get a list with size 1. 我有一个带“”(空白数据)的字符串,每当我尝试用逗号分割字符串时,都会得到一个大小为1的列表。

When the String has no object why am I getting list with size 1. 当字符串没有对象时,为什么我会得到大小为1的列表。

Code : 代码:

String abc = "";
String[] t = abc.split(",");
System.out.println(t.length);

Output : 输出:

1

Because it needs to have a place to put the "" entry. 因为它需要一个放置""项的位置。 split is not a lossy operation (other than losing the delimiters). split不是有损操作(丢失定界符除外)。

From the split documentation split文档

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. 此方法返回的数组包含此字符串的每个子字符串,该子字符串由与给定表达式匹配的另一个子字符串终止或由字符串的结尾终止。 The substrings in the array are in the order in which they occur in this string. 数组中的子字符串按照它们在此字符串中出现的顺序排列。 If the expression does not match any part of the input then the resulting array has just one element, namely this string. 如果表达式与输入的任何部分都不匹配,则结果数组只有一个元素,即此字符串。

When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array. 如果此字符串的开头存在正宽匹配,则在结果数组的开头将包含一个空的前导子字符串。 A zero-width match at the beginning however never produces such empty leading substring. 开头的零宽度匹配永远不会产生这样的空前导子字符串。

(my emphasis) (我的重点)

If you debug inside jdk code of split method, you will find this line from where it returns, 如果您在split方法的jdk代码内部进行调试,则会在返回的行中找到此行,

    // If no match was found, return this
    if (off == 0)
        return new String[]{this};

As the comment says it returns an array containing this , in this case this is a blank string and hence you getting an array of size 1, where the first element is blank ie this 就像评论说的那样,它返回一个包含this的数组,在这种情况下, 是一个空字符串,因此您得到的数组大小为1,其中第一个元素为空白,即this

In this similar example it returns this which is not blank, 在这个类似的示例中,它返回的不是空白,

String abc = "abcd";
String[] t = abc.split(",");
System.out.println(t[0]); // prints "abcd" i.e this

Short Answer: // If no match was found, return this 简短答案: //如果未找到匹配项,则返回此

Here is what written in java official docs. 这是用Java官方文档编写的内容。

 * The array returned by this method contains each substring of this
 * string that is terminated by another substring that matches the given
 * expression or is terminated by the end of the string.  The substrings in
 * the array are in the order in which they occur in this string.  If the
 * expression does not match any part of the input then the resulting array
 * has just one element, namely this string.

So this means that each substring(element that you are placing in the array) is terminated by another substring or end of the string so thatswhy it is giving the size as 1. 因此,这意味着每个子字符串(要放置在数组中的元素)都由另一个子字符串或字符串的结尾终止,因此其大小为1。

Hope this helps to clear your doubt. 希望这有助于清除您的疑问。

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

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