Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join list of string to comma separated and enclosed in single quotes

List<string> test = new List<string>(); test.Add("test's"); test.Add("test"); test.Add("test's more"); string s = string.Format("'{0}'", string.Join("','", test)); 

now the s is 'test's','test','test's more' but I need to replace the inner quotes with 2 single quotes

like this: 'test''s','test','test''s more'

update: I got it to work as below, but I would prefer a cleaner way if possible.

string s = string.Format("`{0}`", string.Join("`,`", test)).Replace("'", "''").Replace("`", "'"); 
like image 818
GaneshT Avatar asked Aug 03 '11 23:08

GaneshT


1 Answers

This should work:

List<string> test = new List<string>();  test.Add("test's");  test.Add("test");  test.Add("test's more"); string s = string.Join("','", test.Select(i => i.Replace("'", "''"))); 

And if you're really looking to enclose the whole thing in single quotes:

string s = string.Format("'{0}'", string.Join("','", test.Select(i => i.Replace("'", "''")))); 
like image 115
Jay Riggs Avatar answered Oct 08 '22 01:10

Jay Riggs