简体   繁体   English

当字符串全部为换行符时,Java按换行符分隔

[英]Java split by newline when string is all newlines

When I have a string like \\n\\n\\n , and I split by \\\\n , I get 0 . 当我有一个像\\n\\n\\n这样的字符串,并且除以\\\\n ,得到0 Why is this? 为什么是这样?

public class Test {

     public static void main(String []args){
        String str = "\n\n\n";
        String[] lines = str.split("\\n");
        System.out.println(lines.length);
     }
}

You can copy & paste the code into CompileOnline . 您可以将代码复制并粘贴到CompileOnline中

The token that you split on is not part of the result. 您分割的令牌不属于结果的一部分。 Since there is nothing else, there is no item to put in the array. 由于没有其他内容,因此没有可放入数组的项目。

This is different when you add another character to your base string though. 但是,当您向基本字符串中添加另一个字符时,情况有所不同。 When you do that, it will include the empty entries after all. 当您这样做时,它将毕竟包括空条目。

This can be explained by looking at the source code in java.lang.String:2305 . 这可以通过查看java.lang.String:2305中的源代码来解释。

Consider the following excerpt: 请考虑以下摘录:

// Construct result
int resultSize = list.size();
if (limit == 0)
     while (resultSize > 0 && list.get(resultSize - 1).length() == 0)
           resultSize--;
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);

If you have 3 empty entries as in your case, resultSize will count down to 0 and essentially return an empty array. 如果您有3个空条目,则resultSize将倒计数为0,并实质上返回一个空数组。

If you have 3 empty entries and one filled one (with the random character you added to the end), resultSize will not move from 4 and thus you will get an array of 4 items where the first 3 are empty. 如果您有3个空条目,并且其中一个填充了一个(您在末尾添加了随机字符),则resultSize不会从4移动,因此您将获得一个包含4个项目的数组,其中前3个为空。

Basically it will remove all the trailing empty values. 基本上,它将删除所有尾随的空值。

String str = "\n\n\n";      // Returns length 0
String str = "\n\n\nb";     // Returns length 4
String str = "\n\n\nb\n\n"; // Returns length 4

As said in the String javadoc : String javadoc中所述:

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. 该方法的工作方式就像通过调用具有给定表达式且限制参数为零的二参数拆分方法。 Trailing empty strings are therefore not included in the resulting array. 因此,结尾的空字符串不包括在结果数组中。

So, when you split() a String made entirely of delimiters (whatever the delimiter is), you will get only empty String s, the delimiter not being included in the result, and, thus, they will all be considered as trailing empty strings, and not be included in the resulting array. 因此,当您对一个完全由定界符组成的String进行split()时(无论定界符是什么),您将只获得空String ,该定界符未包含在结果中,因此,它们都将被视为尾随空字符串,并且不包含在结果数组中。

If you want to get everything, including the empty strings, you have two choices: 如果要获取所有信息,包括空字符串,则有两种选择:

  • add something that is not a delimiter at the end of the String : String的末尾添加不是定界符的内容:

     String str = "\\n\\n\\ne"; String[] lines = str.split("\\\\n"); System.out.println(lines.length); // prints "4" 
  • use the two-argument split method with a negative limit : 使用带有负数limit两参数拆分方法

     String str = "\\n\\n\\n"; String[] lines = str.split("\\\\n", -1); System.out.println(lines.length); // prints "4" 

Because your string contains just \\n 因为您的字符串仅包含\\ n

str.split(""\\n") get the string after \\n which is equivalent to NULL before it's next split search. Therefore you obtain 0 as the lines[] is storing NULL. str.split(“” \\ n“)获得\\ n之后的字符串,该字符串等效于下一次拆分搜索之前的NULL。因此,由于lines []存储NULL时,您将获得0。

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

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