简体   繁体   English

Groovy 字符串到数组

[英]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]]"字符串: def string = "[[asd-123, rthg213],[adw45, gdt345],[fsrfs4, sdferg45]]"

The Array I want: def array = [[asd-123, rthg213],[adw45, gdt345],[fsrfs4, sdferg45]]我想要的数组: def array = [[asd-123, rthg213],[adw45, gdt345],[fsrfs4, sdferg45]]

I have already tried Eval.me, split().我已经尝试过 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 :使用一些简单的正则表达式替换,您可以将 toString() 输出转换为与 Groovy 兼容的列表文字,然后将其提供给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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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