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:
assert (['?'] * 3).join(',') == '?,?,?'
The * n
operation on a list returns a list equal to that list concatenated n times, so ['?'] * 3
equals ['?', '?', '?']
. Then the .join(',')
just joins the elements of that list with a comma.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With