I have a string like: String str = "[aa,bb,cc,dd]"
. I want to convert this to a list in groovy like [aa, bb, cc, dd]
. Any groovy method available for this type conversion?
Using regexp replaceAll
String str = "[aa,bb,cc,dd]"
def a = str.replaceAll(~/^\[|\]$/, '').split(',')
assert a == ['aa', 'bb', 'cc', 'dd']
EDIT:
The following version is a bit more verbose but handles extra white spaces
String str = " [ aa , bb , cc , dd ] "
def a = str.trim().replaceAll(~/^\[|\]$/, '').split(',').collect{ it.trim()}
assert a == ['aa', 'bb', 'cc', 'dd']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With