将单个qoutes添加到具有逗号分隔值的arraylist [英] adding single qoutes to an arraylist having comma separated values

查看:56
本文介绍了将单个qoutes添加到具有逗号分隔值的arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我吗?

can any one please help me on this.

for (int i = 0; i < gridview1.Rows.Count; i++)
{
   string Emp2 = ((TextBox)gridview1.Rows[i].FindControl("TxtEmp")).Text;
   histList.Add(Emp2.ToString());
}
String EmpArrayCsv = String.Join("','", histList.ToArray());


在EmpArrayCsv变量中,我添加了(",")引号并隐藏列表值,但第一个和最后一个变量未用单引号引起来.还有其他方法可以做到这一点,或者我该如何纠正?此值我必须发送到sql以更新表,在该表中它要求值以逗号分隔并放在单引号内.


here in EmpArrayCsv variable i have added the ("'',''") quotes and concating the list values but the first and the last variable are not closed in single quotes. is there any other way to do this or how can i rectify it? this values i have to send to sql to update the table where it requires the values to be in comma separated and inside single quotes.

推荐答案

您将必须添加结果字符串之前和之后的单引号显式表示为String.Join函数,它将通过在值之间放置字符串,"来将数组中的值连接起来,但不会在第一个值和最后一个值之前放置单引号.因此,在您的代码之后追加了另一条语句:

You will have to add the single before and after the result string explicitly as the String.Join function will concatenate the values in array by putting the string '','' in between the values but it will not put single quote before the very fist value and very last value. So after your code append one more statement:

EmpArrayCsv = String.Concat("'",EmpArrayCsv,"'");


希望对您有所帮助.


Hope this helps.


以下代码可用于将项与串联在一起,并用单引号将每个项目括起来

The following code can be used to concatenate the items with , and enclosing each item with single quotes

List<string> histList = new List<string>
{"one","two","three"};

//Using LINQ
string EmpArrayCsv = histList.Aggregate ("",(string agg, string it) =>
    agg +=string.Format("{0} '{1}'", agg == "" ? "" : ",", it ));
Console.WriteLine (EmpArrayCsv);

//Using foreach loop
string EmpArrayCsv2 = "";
foreach(string item in histList)
    EmpArrayCsv2 += string.Format("{0} '{1}'", EmpArrayCsv2 == "" ? "" : ",", item );
Console.WriteLine (EmpArrayCsv2);
//EmpArrayCsv
//'one', 'two', 'three'
//EmpArrayCsv2
// 'one', 'two', 'three'


这篇关于将单个qoutes添加到具有逗号分隔值的arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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