What is the easiest way to create a comma-separated string of n instances of the character c?

In SQL statements, we often need to create a list of question marks that serve as parameters in the IN clause. Which shortest expression GROOVY duplicates a question mark (or any character) n times and connects them with commas to form a string?

Example: expr ('?', 3) will return "?,?,?"

+4
source share
1 answer

I don't know how slickest is, but I like this:

assert (['?'] * 3).join(',') == '?,?,?' 

The operation * n in the list returns a list equal to this list connected n times, therefore ['?'] * 3 is equal to ['?', '?', '?'] . Then .join(',') simply .join(',') elements of this list.

+10
source

Source: https://habr.com/ru/post/1390571/


All Articles