gpt4 book ai didi

没有最终修剪的 Groovy 拆分

转载 作者:行者123 更新时间:2023-12-03 11:49:12 28 4
gpt4 key购买 nike

我正在解析一个 CVS 文件,如下所示:

"07555555555",25.70,18/11/2010,01/03/2011,N,133,0,36,,896,537,547,,Mr,John,Doe,,
"07555555555",10.15,26/01/2011,01/03/2011,N,16,0,100,,896,537,547,,Mrs,Jane,Doe,,jane@doe.com

问题是当使用这样的脚本时:
file.eachLine{ line ->

items = line.split(",")
println items.length
}

结果如下所示:
16
18

这让我觉得 拆分 函数删除最终值。我需要它来拥有所有元素,即使它们是空的。任何的想法?

最佳答案

我想你想做:

items = line.split(',', -1)

确保您获得所有 token

(根据 the javadoc ):

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.

关于没有最终修剪的 Groovy 拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5245912/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com