Adding single quotes and double quotes in an array using groovy

Viewed 30

In groovy, I have an array as below.

def String=[Teacher 1, Teacher 2, Teacher 3]

How can we change this array to

 def String=['"Teacher 1"','"Teacher 2"','"Teacher 3"']

It would be great if you can help me with this.

Thanks a ton in advance!

1 Answers

Ok, I'll bite

Couple of things wrong in the question;

  1. That's a List of Strings, not an array
  2. Using class names as variable names is going to cause you issues

Anyway, back to the question...

Given the list:

def strings = ['Teacher 1', 'Teacher 2', 'Teacher 3']

You can wrap each of them in double quotes by using:

def quotedStrings = strings.collect { '"' + it + '"' }
Related