将逗号分隔列表加入逗号分隔并用单引号括起来

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

现在s是'test's','test','test's more'但我需要用2个单引号替换内部引号

像这样: 'test''s','test','test''s more'

更新:我按照下面的方式工作,但如果可能的话,我希望更清洁。

 string s = string.Format("`{0}`", string.Join("`,`", test)).Replace("'", "''").Replace("`", "'"); 

这应该工作:

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

如果你真的想把整个事情用单引号括起来:

 string s = string.Format("'{0}'", string.Join("','", test.Select(i => i.Replace("'", "''")))); 

这可能比使用string.replace更容易

 string s = "'" + String.Join("','", test) + "'"; 

试试这个:

 string s = string.Join(",", test.Select(x => string.Format("'{0}'", x.Replace("'", "''")))); 

顺便说一句,“测试”中没有撇号 – 撇号不用于复数 。

这不是每个人的口味,但我喜欢为这些类型的任务创建帮助扩展,并将它们放入“实用程序”命名空间:

 public static class ListExtensions { public static void AddDoubleQuoted(this List list, string input) { input = input.Replace("'", "''"); list.Add(input); } } List test = new List(); test.AddDoubleQuoted("test's"); test.AddDoubleQuoted("test"); test.AddDoubleQuoted("test's more"); string s = string.Format("'{0}'", string.Join("','", test)); 

在构建字符串之前,您始终可以编码引号。

我喜欢没有替换的版本:

 using System.Linq; (...) string s = String.Join(", ", from l in MyList select String.Format("'{0}'", l));