Reputation: 983
I am parsing each element of a list one by one.
def List1 = (String[]) Data[2].split(',')
Part of this list gives me a list with elements that contain a delimiter !
.
List1 = [TEST1!01.01.01, TEST2!02.02.02]
I tried to iterate each element of this list and obtain a comma separated list.
def List2 = []
List1.each { List2.add(it.split('!'))}
However, the result was a list of list.
[[TEST1, 01.01.01], [TEST2, 02.02.02]]
Instead of [TEST1, 01.01.01, TEST2, 02.02.02]
.
How do I avoid this and obtain a list as shown above?
Upvotes: 2
Views: 542
Reputation: 983
split()
returns a list. That is the reason why I got a list of list. I found that split()
can carry process multiple delimiters as well when applied with an operator.
The following returns the desired output.
def List1 = (String[]) Data[2].split(',|!')
Upvotes: 0
Reputation: 1009
When you do List2.add(it.split('!'))
, you are adding list to List2
instead of single string because .split()
creates a list from string.
You should firstly create list by using .split()
and than add each member of list to List2
.
Here is solution:
def List1 = ["TEST1!01.01.01", "TEST2!02.02.02"]
def List2 = []
List1.each { List1member ->
def subList = List1member.split('!')
subList.each { subListMember ->
List2.add(subListMember)
}
}
println(List2)
Upvotes: 1
Reputation: 21389
How about this?
def list1 = ['TEST1!01.01.01', 'TEST2!02.02.02']
println list1.collect{it.split('!')}.flatten()
Upvotes: 2