Abdennour TOUMI
Abdennour TOUMI

Reputation: 93193

Partitioning a string to get a list in Groovy

I have the following String :

def s="txtimgtxtvdd"

i want to extract a list from the String above as following

def l=["txt","img","txt","vdd"]

Each 3 consecutive letters is an item of list

Upvotes: 1

Views: 463

Answers (1)

tim_yates
tim_yates

Reputation: 171084

You can use collate (and toList to split the string into a list of chars)

def part = 'txtimgtxtvdd'.toList().collate( 3 )*.join()
assert part == ['txt', 'img', 'txt', 'vdd']

Upvotes: 3

Related Questions