加入字符串逗号分隔的列表,并在单引号 [英] Join list of string to comma separated and enclosed in single quotes

查看:717
本文介绍了加入字符串逗号分隔的列表,并在单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 列表<串GT;测试=新名单<串GT;(); 
test.Add(测试的);
test.Add(测试);
test.Add(测试的更多);
字符串s =的String.Format('{0}'的string.join(,,测试));

现在的s是测试的,测试,测试的更多
,但我需要更换2单引号



这样的内报价:测试''S','测试','测试'更'



更新:我得到了它的如下工作,但我希望如果可能的话更清洁的方式。

 字符串s =的String.Format('{0}'的string.join (``,试))更换(','').Replace(','); 


解决方案

这应该工作:

 列表<串GT;测试=新名单<串GT;(); 
test.Add(测试的);
test.Add(测试);
test.Add(测试的更多);
字符串s =的string.join(',',test.Select(ⅰ= GT; i.Replace(','))ToArray的());



如果你真的希望在单引号整个事情:

 字符串s =的String.Format('{0}'的string.join(,,test.Select(I =方式> i.Replace(','))ToArray的())); 


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

解决方案

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

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

这篇关于加入字符串逗号分隔的列表,并在单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆