Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String variable to a List [Groovy]

How can I convert this String variable to a List ?

def ids = "[10, 1, 9]"

I tried with: as List and toList();

like image 966
user2068981 Avatar asked Sep 03 '25 14:09

user2068981


2 Answers

def l = Eval.me(ids)

Takes the string of groovy code (in this case "[10,1,9]") and evaluates it as groovy. This will give you a list of 3 ints.

like image 171
Rick Mangi Avatar answered Sep 05 '25 02:09

Rick Mangi


def l = ids.split(',').collect{it as int}
like image 20
Sergio Martinez Avatar answered Sep 05 '25 04:09

Sergio Martinez