Search

Dark theme | Light theme

August 13, 2009

Groovy Goodness: String.multiply()

I really like the Groovy extensions to the String class. Today I discoverd the multiply() method. We want to repeat a dash 80 times? Solution:

'-'.multiply(80)

Simple and readable. But it can even be shorter. As John Flinchbaugh mentioned in the comments, the multiply method is nothing more than an overload for the operator *. So we get the same result with the following statement:

'-' * 80