如何将逗号分隔的字符串拆分为空字符串数组 [英] How to split a comma-delimited string into an array of empty strings

查看:180
本文介绍了如何将逗号分隔的字符串拆分为空字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用<$ c将,,,拆分为4 的数组$ c> String.split()

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

这是我的代码:

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

但是,结果标记是一个空数组:[],而不是4的数组 ([,,,])如我所愿。

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

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

I have tested to change the str a little bit:

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

这次结果代币是 [,, , 1] 。这接近我想要的,但我真的不想在进行拆分之前添加这个1。

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.

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

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

你能帮忙解决这个问题吗?

Can you help solve the problem?

推荐答案

你需要使用重载 String #split(正则表达式,限制) 方法,它接受限制参数。

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

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

来自 docs (强调我的):


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

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参数或提供零作为限制, split()将丢弃尾随空字段。当您提供正限制参数时,它会将字段数限制为该特定限制。但是当您提供负限制时, split()方法允许任意数量的字段,也不会丢弃尾随的空字段。为了更清楚,请查看 模式#split(正则表达式,限制) 最后有这个片段(评论已被我添加,但实际源代码中没有。)


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).

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

注意:如果你没有提供任何限制参数,没有limit参数的 split()方法调用重载的 split()方法喜欢这个

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

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

还要注意, 字符串#split(正则表达式,限制) 在内部调用 Pattern#split (正则表达式,限制)

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

这篇关于如何将逗号分隔的字符串拆分为空字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆