Last active
December 21, 2015 14:09
-
-
Save bmakarand2009/6317199 to your computer and use it in GitHub Desktop.
Convert List to Customized String in Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Happy Coding! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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