Java 如何将逗号分隔的字符串拆分为空字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22350683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:08:45  来源:igfitidea点击:

How to split a comma-delimited string into an array of empty strings

javastringsplit

提问by Changwang Zhang

I want to split ",,,"to a array of 4 ""using the String.split()

欲分割",,,"为4的阵列,以""利用String.split()

Here is my code:

这是我的代码:

String str = ",,,";     
String[] tokens = str.split(",");

However, the result tokens were an an empty array: [], rather than an array of 4 ""(["","","",""])as I wanted.

但是,结果标记是一个空数组:[],而不是""(["","","",""])我想要的 4 个数组。

I have tested to change the stra little bit:

我已经测试过str一点点改变:

String str = ",,,1";        
String[] tokens = str.split(",");

This time the result tokens were ["","","","1"]. This is close to what I want, but I really do not want to add this "1" before doing the split.

这次的结果标记是["","","","1"]。这接近我想要的,但我真的不想在拆分之前添加这个“1”。

The problem is basically, the String.split()will return an empty array if it contains only empty elements "".

问题基本上是,String.split()如果它只包含空元素,将返回一个空数组""

Can you help solve the problem?

你能帮忙解决问题吗?

采纳答案by SudoRahul

You need to use the overloaded String#split(regex, limit)method which takes in the limit parameter.

您需要使用String#split(regex, limit)接受 limit 参数的重载方法。

String[] tokens = str.split(",", -1);

From the docs(emphasis mine):

文档(强调我的):

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.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.

limit 参数控制应用模式的次数,因此会影响结果数组的长度。如果限制 n 大于零,则该模式将最多应用 n - 1 次,数组的长度将不大于 n,并且数组的最后一个条目将包含最后一个匹配的分隔符之外的所有输入。如果 n 为非正数,则该模式将被应用尽可能多的次数,并且数组可以具有任意长度。如果 n 为零,则该模式将被应用尽可能多的次数,数组可以具有任意长度,并且将丢弃尾随的空字符串。



Explanation:When you do not provide the limit argument or provide "zero" as the limit, the split()discards trailing empty fields. When you provide a positive limit argument, it limits the number of fields to that particular limit. But when you provide a negative limit, the split()method allows any number of fields and also not discarding the trailing empty fields. To be more clear, have a look at the source code of the Pattern#split(regex, limit)which has this snippet at the end(comments have been added by me and were not present in the actual source code).

说明:当您不提供限制参数或提供“零”作为限制时,将split()丢弃尾随空字段。当您提供正限制参数时,它将字段数限制为该特定限制。但是当您提供负限制时,该split()方法允许任意数量的字段,并且不会丢弃尾随的空字段。为了更清楚,请查看Pattern#split(regex, limit)末尾包含此片段的源代码(注释是我添加的,实际源代码中不存在)。

if (limit == 0) // When zero or no arg is given
    while (resultSize > 0 && matchList.get(resultSize-1).equals("")) // if trailing entries are blank
        resultSize--; // remove them out

Note:If you do not provide any limit argument, the split()method without limit argument calls the overloaded split()method like this.

注意:如果你不提供任何限制参数,split()没有限制参数的split()方法会像这样调用重载方法。

public String[] split(String regex) {
    return split(regex, 0);
}

And also note that, String#split(regex, limit)internally calls the Pattern#split(regex, limit).

还要注意的是,在String#split(regex, limit)内部调用Pattern#split(regex, limit).