Search

Dark theme | Light theme

June 15, 2010

Groovy Goodness: Represent a List as String

In Groovy we can use the toListString() method to get a String representation for a given collection. And since Groovy 1.7.3 we can use a parameter to determine a maximum size of the resulting String. This value is used to stop the String representation after approximately the given size and append three dots (...) to the result.

def list = ['Groovy', 'Clojure', 'Java']

def listString = list.toListString()
def abbreviated = list.toListString(12)

assert '[Groovy, Clojure, Java]' == listString
assert '[Groovy, Clojure, ...]' == abbreviated