拆分函数没有正确拆分字符串[重复]

时间:2023-02-14 18:08:28

This question already has an answer here:

这个问题在这里已有答案:

I have a string with values separated by a ;

我有一个字符串,其值由a分隔;

col1;col2;;

The last value is empty. I'm getting the number of columns by splitting the string:

最后一个值为空。我通过拆分字符串得到列数:

int columns = myString.split(";").length;

However, the above returns 2 instead of 3.

但是,上面的返回2而不是3。

Is this a problem of the split method?

这是拆分方法的问题吗?

1 个解决方案

#1


3  

Read the Javadoc :

阅读Javadoc:

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方法一样。因此,结尾的空字符串不包含在结果数组中。

You can use myString.split(";",-1) to get an array with trailing empty strings.

您可以使用myString.split(“;”, - 1)来获取具有尾随空字符串的数组。

#1


3  

Read the Javadoc :

阅读Javadoc:

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方法一样。因此,结尾的空字符串不包含在结果数组中。

You can use myString.split(";",-1) to get an array with trailing empty strings.

您可以使用myString.split(“;”, - 1)来获取具有尾随空字符串的数组。