简体   繁体   中英

Groovy String to Array

I have a string and I just want the string as array.

The String: def string = "[[asd-123, rthg213],[adw45, gdt345],[fsrfs4, sdferg45]]"

The Array I want: def array = [[asd-123, rthg213],[adw45, gdt345],[fsrfs4, sdferg45]]

I have already tried Eval.me, split(). But I did not found a solution. Please help, Thank you!

Using some simple regex replacement, you can turn your toString()-output to a Groovy-compatible list-literal, and then feed it to Eval.me :

def string = "[[asd-123, rthg213],[adw45, gdt345],[fsrfs4, sdferg45]]"
def list = Eval.me string.replaceAll( /\b([\w\d-]+)\b/, '"$1"' )

assert list.class == ArrayList
assert list[ 1 ][ 1 ] == 'gdt345'

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