Gan*_*shT 32 c# string string-formatting c#-4.0
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));
Run Code Online (Sandbox Code Playgroud)
现在s是,'test's','test','test's more'
但我需要用2个单引号替换内部引号
像这样: 'test''s','test','test''s more'
更新:我得到它如下工作,但我希望如果可能的话更清洁.
string s = string.Format("`{0}`", string.Join("`,`", test)).Replace("'", "''").Replace("`", "'");
Run Code Online (Sandbox Code Playgroud)
Jay*_*ggs 53
这应该工作:
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("'", "''")));
Run Code Online (Sandbox Code Playgroud)
如果你真的想把整个事情用单引号括起来:
string s = string.Format("'{0}'", string.Join("','", test.Select(i => i.Replace("'", "''"))));
Run Code Online (Sandbox Code Playgroud)
小智 23
这可能比使用更容易 string.replace
string s = "'" + String.Join("','", test) + "'";
Run Code Online (Sandbox Code Playgroud)
小智 6
string s = string.Join(",", itemsList.Select(i => $"'{i}'"));
Run Code Online (Sandbox Code Playgroud)
使用字符串插值。String.Replace这里不需要。