有时候,我们的一些业务数据,有些定义的是按某个分割符分割数据,然后一行一行的,处理这种数据时候,要务必小心,因为它简单,不用维护类似json格式的数据或者一个对象,而是直接通过下标位置来访问数据的,相信这种场景大家也都接触过,当然弊端也是显而易见的,如果位置放错或者代码使用不当,都会造成一些问题,所以使用这种方式时,一会都会约定一些内容,比如行分隔符,列分割符等,下面来看一个小问题,先看下面一段代码:
Java代码
运行完,你会发现第二段代码的数组的长度竟然与你想的不一致?为什么?我们来看 官网api解释:
Java代码
如果limit是负数,那么它会尽可能匹配到所有的这个数组的个数,如果是0那么尽可能匹配出现的元素,但是空字符串将会被就丢弃,但我们不希望它丢弃,因为丢弃就意味着有业务字段缺失,所以来看下最终的写法:
Java代码