将字符串列表连接到逗号分隔并用单引号引起来

发布于 2024-11-27 16:45:45 字数 504 浏览 1 评论 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));

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

べ繥欢鉨o。 2024-12-04 16:45:45

这应该可行:

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

如果您真的想将整个内容用单引号引起来:

string s = string.Format("'{0}'", string.Join("','", test.Select(i => i.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("'", "''")));

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("'", "''"))));
云之铃。 2024-12-04 16:45:45

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

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

This may be easier than using string.replace

string s = "'" + String.Join("','", test) + "'";
凯凯我们等你回来 2024-12-04 16:45:45
string s = string.Join(",", itemsList.Select(i => $"'{i}'"));

使用字符串插值。这里不需要String.Replace

string s = string.Join(",", itemsList.Select(i => 
quot;'{i}'"));

Using string interpolation. String.Replace is not required here.

忆悲凉 2024-12-04 16:45:45

试试这个:

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

顺便说一句,“tests”中没有撇号 - 撇号不用于复数

Try this:

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

By the way, there's no apostrophe in "tests" - apostrophes aren't used for plurals.

绝情姑娘 2024-12-04 16:45:45

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

public static class ListExtensions
{
   public static void AddDoubleQuoted(this List<string> list, string input)
   {
     input = input.Replace("'", "''");
     list.Add(input);
   }
}

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

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<string> list, string input)
   {
     input = input.Replace("'", "''");
     list.Add(input);
   }
}

List<string> test = new List<string>();
test.AddDoubleQuoted("test's");
test.AddDoubleQuoted("test");
test.AddDoubleQuoted("test's more");
string s = string.Format("'{0}'", string.Join("','", test));
凹づ凸ル 2024-12-04 16:45:45
string.Join(",", listofstring.Select(x=> 
quot;'{x}'"))
string.Join(",", listofstring.Select(x=> 
quot;'{x}'"))
沒落の蓅哖 2024-12-04 16:45:45

我喜欢没有替换的版本:

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

I like a version without Replace:

using System.Linq;
(...)
string s = String.Join(", ", from l in MyList select String.Format("'{0}'", l));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文