How to assign a value to a variable from a string divided by groovy?

I want to assign an element of an array to a variable directly using groovy as follows:

def str = "xyz=abc" def [name, value] = str.split("=") 

but he doesn’t like groovy. Is there any way to do this (do not store the result of the array and get index [0], index [1] from it?).

Thanks,

+8
groovy
source share
2 answers

You need brackets instead of brackets:

 def str = "xyz=abc" def (name, value) = str.split("=") 

enter image description here

Please note that you need to know how many items you expect, or you will get unexpected results.

+15
source share
 def name, value (name,value) = str.split("=") 

You just need to make your determination before your multiple appointment.

+3
source share

All Articles