今天遇到一个BUG,给定数据"a,b,c,d,e“,用"a,b,c,d,e“.split(",")函数生成的数组长度是5。但是,如果给定的数组是“a,b,c,d,”,这种格式,情况就不一样了,我之前一直认为"a,b,c,d,“.split(",")的数组长度是5,但是事实上,长度是4。
参考代码片段:
String a = "a,b,c,d,"; String b = "a,b,c,d,e"; System.out.println(a.split(",").length); System.out.println(b.split(",").length);
于是查看String.split的源代码:
public String[] split(String regex) { return split(regex, 0); }
找到方法说明里面有这么一段:
写道
Splits this string around matches of the given regular expression.
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.
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.
标记红色的这句话告诉我,在末尾的空字符串不会被包含在结果数组。现在应该很清楚了,可能是我愚笨了,理解错误了,在此标记一下。
那么如果遇到“a,b,c,d,”这种数据,想要让返回的数组包含最后的空字符串,即数组长度是5,有没有办法呢?
接着查看JDK的源码,找到split的重载方法:
public String[] split(String regex, int limit) { return Pattern.compile(regex).split(this, limit); }
这个方法说明中有一段话:
写道
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.
这段话告诉我有两种方式可以实现我的需求:
“a,b,c,d,”.split(",",-1); “a,b,c,d,”.split(",",5);
limit要么是负数,要么大于等于result array的长度。
相关推荐
`split()`方法的例子是`String [] result = orgStr.split(",");`,而`StringTokenizer`的例子是创建一个迭代器并逐个提取分隔的项。 5. **StringBuffer与StringBuilder的区别**: 这两者都是用于动态构建字符串的...
### 华为OD机试C卷 - 最大N个数与最小N个数的和(Java & JS & Python) #### 風题描述 在本题中,我们需要编写一个程序来...这种方法不仅可以解决当前问题,还可以应用于其他涉及查找数组中特定数量的元素的问题。
result.extend(array[i:i+length]) return result arrays = [] length = int(input()) for _ in range(2): arrays.append(list(map(int, input().split(',')))) print(','.join(map(str, merge_arrays(arrays, ...
JavaScript提供了许多内置的String对象方法,用于处理字符串,如`length`获取长度,`charAt(index)`获取指定索引处的字符,`substring(startIndex, endIndex)`截取子字符串,`indexOf(searchValue[, fromIndex])`...
在JavaScript(简称JS)中,字符串是编程时经常打交道的数据类型之一。字符串是由零个或多个字符组成的不可变序列,可以用来表示文本信息。本文将深入探讨JavaScript中处理字符串的各种常见操作。 一、创建字符串 ...