拆分字符串时数组越界异常

Array out of bound exception while splitting the String

使用“,”拆分字符串时,出现数组越界异常。请在下面找到程序。

public static void main(String[] args) {
    String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
    System.out.println(c.split(",")[11]);
}

即使第 11 个元素为空我也想打印空字符串(因为在某些记录中第 11 个元素不为空)。 请帮助我调试错误。

可能你想要

c.split(",", -1);

这将在末尾保留空字符串。

假设拆分后生成的数组是splittedArray,它会是这样的 -

String splittedArray = {"1","10k ABC D", "XYZ" "AB","", "" "","","12345"};

可以看到splittedArray[11]处没有元素。 splittedArray 的大小是 9。如果你像这样固定索引 11 那么就不会发生错误 -

String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",")[8]);

输出:
12345

c.split(",") 的结果是一个包含 9 个元素的字符串数组。如果您使用此代码,您可以查看此内容:

String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
    String[] s = c.split(",");

    for(int i = 0; i < s.length; i++){
        System.out.println(i);
        System.out.println(s[i]);
    }

如果您想要除 ',' 字符之外的所有内容,则必须在拆分中使用“,+”而不是“,”。此正则表达式 4 结果及其作用是忽略所有 ','.

看看 split 中的方法描述 String class:

This method works as if by invoking the two-argument method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

因此,split 从数组中删除 trailing 个空字符串。在您的示例中,生成的数组是:

'1'
'10k ABC D'
' XYZ AB'
''
''
''
''
''
'12345'

如果您要拆分以下字符串(在最后一个逗号前添加 space):

"1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,"

结果数组将是:

'1'
'10k ABC D'
' XYZ AB'
''
''
''
''
''
'12345'
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
' '

如果在没有任何参数的情况下应用 Java split 方法,它会丢弃尾随的空元素。因此,如果您在将结果值分配给数组时进行调试,如下所示,您会注意到结果数组中只有 9 个元素以元素“12345”结尾。

String[] arr = c.split(",");

你可以像下面这样传递一个limit参数(如果limit参数是非正数,那么模式将被应用尽可能多的次数,数组可以有任意长度),这样split方法将return 带有尾随空元素的数组。

public static void main(String[] args) {
   String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
   System.out.println(c.split(",", -1)[11]);
}

希望以下代码对您有所帮助

public static void main(String[] args) {
    String cvsSplitBy =",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
    String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
    String sTemp[] = c.split(cvsSplitBy);
    for(int i=0;i<sTemp.length;i++){
        if(((String) c.split(cvsSplitBy)[i])!=null && (!"".equals((String) c.split(cvsSplitBy)[i].trim())))
    System.out.println((String) c.split(cvsSplitBy)[i]);
    }

以上代码的输出

1
10k ABC D
XYZ AB
12345

根据split documentation :

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.

这由 java.util.regex.Pattern 中的方法 split 的源代码验证,由 String

split 使用
if (limit == 0)
    while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
        resultSize--;

你的数组实际大小是9

试试这个对你有帮助。

String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",",-1)[11]);

试试这个!

String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",",-1)[11]);