简体   繁体   English

在C#中将数组列表转换为逗号分隔的字符串

[英]Convert Array List to Comma Separated String in C#

I'm using String.Join to attempt to turn an array list into a string that is comma separated, such as 我正在使用String.Join尝试将数组列表转换为以逗号分隔的字符串,例如

xxx@xxx.com,yyy@xxx.com,zzz@xxx.com,www@xxx.com

I can't seem to get the syntax working. 我似乎无法使语法正常工作。

Here's what I'm trying: 这是我正在尝试的:

    for (i = 0; i < xxx; i++)
    {
        MailingList = arrayList[i].ToString();
        MailingList = string.Join(", ", MailingList.ToString());
        Response.Write(MailingList.ToString());
    }

Can you help me? 你能帮助我吗?

Thank you in advance- 先感谢您-

Guessing from the name of your variable ( arrayList ), you've got List<string[]> or an equivalent type there. 从变量名( arrayList )猜测,您已经有了List<string[]>或等效类型。

The issue here is that you're calling ToString() on the array. 这里的问题是您要在数组上调用ToString() Try this instead: 尝试以下方法:

for (i = 0; i < xxx; i++)
{
    var array = arrayList[i];
    MailingList = string.Join(", ", array);
    Response.Write(MailingList);
}

EDIT: If arrayList is simply an ArrayList containing strings, you can just do 编辑:如果arrayList只是包含字符串的ArrayList ,则可以执行

Response.Write(string.Join(", ", arrayList.OfType<string>()));

Personally I would avoid using nongeneric collections (such as ArrayList ) if possible and use strongly-typed collections from System.Collections.Generic such as List<string> . 就个人而言,我将尽可能避免使用非通用集合(例如ArrayList ),而应使用System.Collections.Generic强类型集合(例如List<string> For example, if you have a piece of code that depends on that all contents of the ArrayList are strings, it will suffer catastrophically if you accidentally add an item that's not a string. 例如,如果您有一段代码依赖于 ArrayList所有内容都是字符串,那么如果您不小心添加了不是字符串的项目,它将遭受灾难性的影响。

EDIT 2: If your ArrayList actually contains System.Web.UI.WebControls.ListItem s like you mentioned in your comment: arrayList.AddRange(ListBox.Items); 编辑2:如果您的ArrayList实际上包含System.Web.UI.WebControls.ListItem就像您在注释中提到的那样: arrayList.AddRange(ListBox.Items); , then you'll need to use this instead: ,那么您将需要使用它:

Response.Write(string.Join(", ", arrayList.OfType<ListItem>()));

The second parameter for String.Join needs to be an IEnumerable . String.Join的第二个参数必须是IEnumerable Replace MailingList.ToString() with arrayList and it should work. 更换MailingList.ToString()arrayList ,它应该工作。

Initialization: 初始化:

string result = string.Empty;

For value types: 对于值类型:

if (arrayList != null) {
   foreach(var entry in arrayList){
      result += entry + ',';
   }
}

For reference types: 对于参考类型:

if (arrayList != null) {
   foreach(var entry in arrayList){
      if(entry != null)
         result += entry + ',';
   }
}

And cleanup: 并清理:

if(result == string.Empty)
   result = null;
else
   result = result.Substring(0, result.Length - 1);

most of the answers are already there, still posting a complete - working snippet 大多数答案已经存在,仍在发布完整的工作片段

string[] emailListOne = { "xxx@xxx.com", "yyy@xxx.com", "zzz@xxx.com", "www@xxx.com" };
string[] emailListTwo = { "xxx@xxx1.com", "yyy@xxx1.com", "zzz@xxx1.com", "www@xxx1.com" };
string[] emailListThree = { "xxx@xxx2.com", "yyy@xxx2.com", "zzz@xxx2.com", "www@xxx.com" };
string[] emailListFour = { "xxx@xxx3.com", "yyy@xxx3.com", "zzz@xxx3.com", "www@xxx3.com" };

List<string[]> emailArrayList = new List<string[]>();
emailArrayList.Add(emailListOne);
emailArrayList.Add(emailListTwo);
emailArrayList.Add(emailListThree);
emailArrayList.Add(emailListFour);

StringBuilder csvList = new StringBuilder();
int i = 0;
foreach (var list in emailArrayList)
{
   csvList.Append(string.Join(",", list));
   if(i < emailArrayList.Count - 1)
      csvList.Append(",");
   i++;
}
Response.Write(csvList.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM