Findall 与字符串数组 groovy
Findall with array of string groovy
我有一个字符串 /sample/data
。当我使用 split
拆分时,我得到以下结果,
["","sample","data"]
我想忽略空字符串。所以我尝试了下面的代码,
"/sample/data".split('/').findAll(it != "")
它给我一个错误“cannot call String[] findAll with argument bool
”。
如何拆分并获得其中没有空字符串的列表?
您可以进行以下操作:
println "/sample/data".split('/').findAll {it}
findAll {it}
将获取所有非空值。
拆分方法returns数组。
如果需要List,使用tokenize
"/sample/data".tokenize('/')
在这种情况下您也不需要使用 findAll。
Parens 会起作用(请参阅问题评论)。所以你的解决方案已经接近:
"/a/b".split("/").findAll()
因为大多数 Groovy 函数的元数为零,这将调用带有 identity 闭包的函数。由于空字符串被认为是错误的,因此这会将它们过滤掉。
我有一个字符串 /sample/data
。当我使用 split
拆分时,我得到以下结果,
["","sample","data"]
我想忽略空字符串。所以我尝试了下面的代码,
"/sample/data".split('/').findAll(it != "")
它给我一个错误“cannot call String[] findAll with argument bool
”。
如何拆分并获得其中没有空字符串的列表?
您可以进行以下操作:
println "/sample/data".split('/').findAll {it}
findAll {it}
将获取所有非空值。
拆分方法returns数组。 如果需要List,使用tokenize
"/sample/data".tokenize('/')
在这种情况下您也不需要使用 findAll。
Parens 会起作用(请参阅问题评论)。所以你的解决方案已经接近:
"/a/b".split("/").findAll()
因为大多数 Groovy 函数的元数为零,这将调用带有 identity 闭包的函数。由于空字符串被认为是错误的,因此这会将它们过滤掉。