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

后端 未结 6 1255
旧时难觅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 20:08

    It isn't to everyone's taste, but I like to create helper extensions for these kinds of tasks, and put them into a "utility" namespace:

    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));
    

提交回复
热议问题