简体   繁体   中英

What's the slickest way to create a comma-separated string of n instances of character c?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM