While splitting the String using ',' I am getting Array out of bound exception. Please find below the program.
使用','拆分String时,我得到的数据超出绑定异常。请在下面的程序中找到。
public static void main(String[] args) {
String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",")[11]);
}
Even the 11th element is null i want to print the null string (Because in some records the 11th element is not null). Kindly help me to debug the error.
即使第11个元素为null我想打印空字符串(因为在某些记录中第11个元素不为null)。请帮我调试错误。
9 个解决方案
#1
Take a look at split
method description in String
class:
看一下String类中的split方法描述:
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.
此方法就像通过使用给定表达式和limit参数为零调用双参数方法一样工作。因此,结尾的空字符串不包含在结果数组中。
So, split
get rid of trailing empty strings from the array. In your example, the resulting array is:
因此,拆分从数组中删除尾随的空字符串。在您的示例中,结果数组是:
'1'
'10k ABC D'
' XYZ AB'
''
''
''
''
''
'12345'
If you would split following string (with a space before last comma):
如果你要拆分跟随字符串(在最后一个逗号之前有一个空格):
"1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,"
“1,10k ABC D,XYZ AB ,,,,,, 12345 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,”,“
the resulting array would be:
结果数组将是:
'1'
'10k ABC D'
' XYZ AB'
''
''
''
''
''
'12345'
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
' '
#2
Probably you want
可能你想要的
c.split(",", -1);
This will keep empty strings at the end.
这将在结尾处保留空字符串。
#3
try this will help you.
试试这会对你有所帮助。
String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",",-1)[11]);
#4
Try this one!
试试这个吧!
String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",",-1)[11]);
#5
The Result of c.split(",")
is a String array with 9 elements. You can view this if you use this code:
c.split(“,”)的结果是一个包含9个元素的String数组。如果您使用此代码,则可以查看此内容:
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]);
}
If you want everything besides the ',' char you have to use ",+" instead of "," in the split. This regular expression 4 results and what it does is ignoring all the ','.
如果你想要除','char之外的所有东西,你必须在分割中使用“,+”而不是“,”。这个正则表达式4结果,它的作用是忽略所有','。
#6
Suppose after splitting the generated array is splittedArray
and it will be like this -
假设在拆分后生成的数组是splittedArray,它将是这样的 -
String splittedArray = {"1","10k ABC D", "XYZ" "AB","", "" "","","12345"};
You can see there is not element at splittedArray[11]
. The size of the splittedArray
is 9. If you fix the index 11 like this then no error will be occurred -
你可以看到splittedArray [11]中没有元素。 splittedArray的大小是9.如果您像这样修复索引11,那么不会发生错误 -
String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",")[8]);
Output:
12345
#7
If Java split method is applied without any argument, it discards trailing null elements. So if you debug while assigning the resulted values to an array as follows you will notice that there are only 9 elements in the resulted array which ends with element "12345".
如果在没有任何参数的情况下应用Java split方法,则会丢弃尾随null元素。因此,如果在将结果值分配给数组时进行调试,则会发现结果数组中只有9个元素以元素“12345”结尾。
String[] arr = c.split(",");
You can pass a limit parameter as follows (If limit parameter is non-positive then the pattern will be applied as many times as possible and the array can have any length), so that the split method will return an array with trailing null elements.
您可以按如下方式传递limit参数(如果limit参数为非正数,则模式将被应用尽可能多次,并且数组可以具有任意长度),以便split方法将返回具有尾随null元素的数组。
public static void main(String[] args) {
String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",", -1)[11]);
}
#8
Hope the below code helps
希望以下代码有所帮助
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]);
}
Output for above code
上述代码的输出
1
10k ABC D
XYZ AB
12345
1 10k ABC D XYZ AB 12345
#9
According to 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.
此方法的作用就像通过调用具有给定表达式和limit参数为零的双参数split方法一样。因此,结尾的空字符串不包含在结果数组中。
This is validated by the source code of the method split
in java.util.regex.Pattern, used by the split
of String
这由java.util.regex.Pattern中分割的方法的源代码验证,由String的分割使用
if (limit == 0)
while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
resultSize--;
The actual size of your array is 9
数组的实际大小为9
#1
Take a look at split
method description in String
class:
看一下String类中的split方法描述:
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.
此方法就像通过使用给定表达式和limit参数为零调用双参数方法一样工作。因此,结尾的空字符串不包含在结果数组中。
So, split
get rid of trailing empty strings from the array. In your example, the resulting array is:
因此,拆分从数组中删除尾随的空字符串。在您的示例中,结果数组是:
'1'
'10k ABC D'
' XYZ AB'
''
''
''
''
''
'12345'
If you would split following string (with a space before last comma):
如果你要拆分跟随字符串(在最后一个逗号之前有一个空格):
"1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,"
“1,10k ABC D,XYZ AB ,,,,,, 12345 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,”,“
the resulting array would be:
结果数组将是:
'1'
'10k ABC D'
' XYZ AB'
''
''
''
''
''
'12345'
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
' '
#2
Probably you want
可能你想要的
c.split(",", -1);
This will keep empty strings at the end.
这将在结尾处保留空字符串。
#3
try this will help you.
试试这会对你有所帮助。
String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",",-1)[11]);
#4
Try this one!
试试这个吧!
String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",",-1)[11]);
#5
The Result of c.split(",")
is a String array with 9 elements. You can view this if you use this code:
c.split(“,”)的结果是一个包含9个元素的String数组。如果您使用此代码,则可以查看此内容:
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]);
}
If you want everything besides the ',' char you have to use ",+" instead of "," in the split. This regular expression 4 results and what it does is ignoring all the ','.
如果你想要除','char之外的所有东西,你必须在分割中使用“,+”而不是“,”。这个正则表达式4结果,它的作用是忽略所有','。
#6
Suppose after splitting the generated array is splittedArray
and it will be like this -
假设在拆分后生成的数组是splittedArray,它将是这样的 -
String splittedArray = {"1","10k ABC D", "XYZ" "AB","", "" "","","12345"};
You can see there is not element at splittedArray[11]
. The size of the splittedArray
is 9. If you fix the index 11 like this then no error will be occurred -
你可以看到splittedArray [11]中没有元素。 splittedArray的大小是9.如果您像这样修复索引11,那么不会发生错误 -
String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",")[8]);
Output:
12345
#7
If Java split method is applied without any argument, it discards trailing null elements. So if you debug while assigning the resulted values to an array as follows you will notice that there are only 9 elements in the resulted array which ends with element "12345".
如果在没有任何参数的情况下应用Java split方法,则会丢弃尾随null元素。因此,如果在将结果值分配给数组时进行调试,则会发现结果数组中只有9个元素以元素“12345”结尾。
String[] arr = c.split(",");
You can pass a limit parameter as follows (If limit parameter is non-positive then the pattern will be applied as many times as possible and the array can have any length), so that the split method will return an array with trailing null elements.
您可以按如下方式传递limit参数(如果limit参数为非正数,则模式将被应用尽可能多次,并且数组可以具有任意长度),以便split方法将返回具有尾随null元素的数组。
public static void main(String[] args) {
String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",", -1)[11]);
}
#8
Hope the below code helps
希望以下代码有所帮助
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]);
}
Output for above code
上述代码的输出
1
10k ABC D
XYZ AB
12345
1 10k ABC D XYZ AB 12345
#9
According to 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.
此方法的作用就像通过调用具有给定表达式和limit参数为零的双参数split方法一样。因此,结尾的空字符串不包含在结果数组中。
This is validated by the source code of the method split
in java.util.regex.Pattern, used by the split
of String
这由java.util.regex.Pattern中分割的方法的源代码验证,由String的分割使用
if (limit == 0)
while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
resultSize--;
The actual size of your array is 9
数组的实际大小为9