[英]Findall with array of string groovy
I have a string /sample/data
. 我有一个字符串/sample/data
。 When I split using split
I get the following result, 当我使用split
我得到以下结果,
["","sample","data"]
I want to ignore the empty string(s). 我想忽略空字符串。 So I tried the following code, 所以我尝试了以下代码,
"/sample/data".split('/').findAll(it != "")
It gives me an error " cannot call String[] findAll with argument bool
". 它给我一个错误“ cannot call String[] findAll with argument bool
”。
How can I split and get a List without empty string in it? 如何拆分并获取没有空字符串的列表?
split method returns array. split方法返回数组。 If you need List, use tokenize 如果需要列表,请使用标记化
"/sample/data".tokenize('/')
also you don't need to use findAll in this case. 在这种情况下,您也不需要使用findAll。
You can do as below: 您可以执行以下操作:
println "/sample/data".split('/').findAll {it}
findAll {it}
would fetch all the non empty values. findAll {it}
将获取所有非空值。
Parens would work (see comments on question). Parens会工作(请参阅问题注释)。 So your solution is already close: 因此,您的解决方案已经接近:
"/a/b".split("/").findAll()
Because most of the Groovy functions have a zero arity, which will call the function with an identity closure. 因为大多数Groovy函数的arity为零,所以它将使用一个标识闭包来调用该函数。 And since an empty string is considered falsey, this will filter them out. 由于空字符串被认为是错误的,因此将其过滤掉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.