当处理一个CSV字符串空决赛场上,myString.split(",")返回数组项的数量不正确 [英] When processing a csv String with an empty final field, myString.split( "," ) returns incorrect number of array entries

查看:110
本文介绍了当处理一个CSV字符串空决赛场上,myString.split(",")返回数组项的数量不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理某行从.csv文件中读取使用 String.split()并找到最终的空字符串后,最后一个分隔符是不使它成为了拆分创建数组的功能。

I'm processing a certain line read from a .csv file using String.split(",") and finding that the final empty string after the last delimiter is not making it into the array created by the split function.

下面是导致该错误的变量值:

Here are the variable values that are causing the error:

String toSplit = "1,Some Value,31337,Another Value,";
String[] values = toSplit.split( "," );

阵列最终具有比数组项的预计数较少。创建数组是:

The values array ends up having fewer than the expected count of array entries. The array created is:

values[0] : '1'
values[1] : 'Some Value'
values[2] : '31337'
values[3] : 'Another Value'

值[4] 抛出ArrayIndexOutOfBounds异常情况。

with a values[4] throwing an ArrayIndexOutOfBounds exception.

我想该数组是:

values[0] : '1'
values[1] : 'Some Value'
values[2] : '31337'
values[3] : 'Another Value'
values[4] : ''

警告:我读的另一.csv文件创建者的输出,并且不希望使用字符串分隔符或抛出一个'空白字符,如果相应的数据是空的地方。 (即不希望: toSplit =1,一定的价值,31337,另一种价值,用空格​​就结束)

One caveat: I am reading the output of another .csv file creator and don't want to use string delimiters or throw a ' ' whitespace character into places where the data is empty. (ie. do not want: toSplit = "1,Some Value,31337,Another Value, " with whitespace on the end.)

这是在String.split(错误),是有一种变通方法/另一种选择?

Is this a bug in String.split() and is there a workaround/another option?

推荐答案

想通了,因为我是进入的问题!使用 stringObj.split(,numDelimitersExpected)或,作为@Supericy指出, stringObj.split(,,-1)

Figured it out as I was entering the question! Use stringObj.split( ",", numDelimitersExpected ) or, as @Supericy pointed out, stringObj.split( ",", -1 ).

据有关<一个Android的文档href="http://developer.android.com/reference/java/util/regex/Pattern.html#split%28java.lang.CharSequence%29"相对=nofollow> stringObj.split(分隔符)调用 line.split()相当于调用 line.split(,0)的第二个参数指的是限制,这台'的最大的数项的,并定义了治疗尾随空字符串的。看着对文件<一个href="http://developer.android.com/reference/java/util/regex/Pattern.html#split%28java.lang.CharSequence%29"相对=nofollow> stringObj.split(分隔符)它指出与限制== 0 结尾空字符串将不予退还

According to the Android docs about stringObj.split( delimiter ) a call to line.split( "," ) is equivalent to a call to line.split( ",", 0 ) with the second argument referring to limit, which sets the 'maximum number of entries' and defines the 'treatment of trailing empty strings' . Looking at the documentation for stringObj.split( delimiter ) it states that with limit == 0 'trailing empty strings will not be returned'.

该解决方案是使用调用拆分(,限)限制设置为您希望数组项的数量找回。在我的情况下,限制设置为5返回数组

The solution is to use a call to split( ",", limit ) with limit set to the number of array entries you expect to get back. In my case a limit set to 5 returns the array

values[0] : '1'
values[1] : 'Some Value'
values[2] : '31337'
values[3] : 'Another Value'
values[4] : ''

这正是我想要的。 问题解决了。

事实上,即使你的 toSplit 字符串比限制少分隔符,有一组<调用code>限制仍然会创建空数组项高达限制。例如,与相同的输入字符串在我的问题:

In fact, even if your toSplit string has less delimiters than limit, a call with a set limit will still create empty array entries up to limit. For example, with the same input string as in my question:

String toSplit = "1,Some Value,31337,Another Value,"

呼叫字符串值[] = toSplit.split(,,8)返回数组

values[0] : '1'
values[1] : 'Some Value'
values[2] : '31337'
values[3] : 'Another Value'
values[4] : ''
values[5] : ''
values[6] : ''
values[7] : ''

整齐!

注:Oracle的Java的 String.split(分隔符)具有相同的功能与Android的文档赢得他们的更简洁的解释。 (:

Note: Oracle's Java String.split( delimiter ) has the same functionality with the Android docs winning for their more concise explanation. (:

编辑:作为@Supericy增加, toSplit.split(,,-1)也要正确返回尾随空条目。感谢您的加入!

As @Supericy added, toSplit.split( ",", -1 ) would also properly return the trailing empty entry. Thanks for the addition!

这篇关于当处理一个CSV字符串空决赛场上,myString.split(&QUOT;,&QUOT;)返回数组项的数量不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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