简体   繁体   中英

Findall with array of string groovy

I have a string /sample/data . When I split using split I get the following result,

["","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 ".

How can I split and get a List without empty string in it?

split method returns array. If you need List, use tokenize

"/sample/data".tokenize('/')

also you don't need to use findAll in this case.

You can do as below:

println "/sample/data".split('/').findAll {it}

findAll {it} would fetch all the non empty values.

Parens would work (see comments on question). 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. And since an empty string is considered falsey, this will filter them out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM