TP_JAVA
TP_JAVA

Reputation: 1002

How to get values in a List with single quote and comma separated from using groovy?

I've a List of keys and want to get them in a string with Single quotes and comma separated values..

def keys= [A,B,C,D]

the result should be String values = 'A','B','C','D' as I need to feed these values to groovy.sql query.

Is there a way to do in a simple way to achieve this?

Upvotes: 1

Views: 3407

Answers (1)

tim_yates
tim_yates

Reputation: 171054

You mean like:

String s = keys.collect { "'$it'" }.join( ',' )

Be careful though. Escaping your own sql is rarely a good idea, can't you use prepared statements? Or the groovy Sql class? Both of these handle escapes for you

Upvotes: 6

Related Questions