Skip to content

Instantly share code, notes, and snippets.

@bmakarand2009
Last active December 21, 2015 14:09
Show Gist options
  • Save bmakarand2009/6317199 to your computer and use it in GitHub Desktop.
Save bmakarand2009/6317199 to your computer and use it in GitHub Desktop.
Convert List to Customized String in Groovy
//Converts a list say
//List myList = ["user", "pwd","address1", "address2"] to
//String resultStr = 'user','pwd','address1','address2'
//VERSION 1 of Code Written in Groovy but Inspired by Java
//Even thought the following Code is written in Groovy its, 100% of how problem would have been in Java
if(!myList.empty)
{
//takes out the square bracket from either sides
String resultStr=myList.substring(1, list_all_user_contacts.length()-1)
//Relaces the "double quotes with ","
resultStr=resultStr.replaceAll(",", "','")
//Appends apostoles ' at the start and end
resultStr="'"+resultStr+"'"
println resultStr
}
//Version2 : PURE Groovy, Checkout the Magic
String quote="'"
String resultStr="""$quote${myList.join("','")}$quote """
println resultStr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment