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

后端 未结 6 1169
旧时难觅i
旧时难觅i 2021-02-03 19:41
List test = new List();
test.Add(\"test\'s\");
test.Add(\"test\");
test.Add(\"test\'s more\");
string s = string.Format(\"\'{0}\'\", string.J         


        
6条回答
  •  无人共我
    2021-02-03 19:45

    This should work:

    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("'", "''")));
    

    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("'", "''"))));
    

提交回复
热议问题