In SQL statements, we often need to create a list of question marks that serve as parameters in an IN clause. What’s the shortest GROOVY expression to duplicate a question mark (or any character) n times and join them with commas to form a string?
Example:
expr(‘?’, 3) would return “?,?,?”
I don’t know if the slickest, but I like this:
The
* noperation on a list returns a list equal to that list concatenated n times, so['?'] * 3equals['?', '?', '?']. Then the.join(',')just joins the elements of that list with a comma.