Reputation: 1407
I'm using String.Join to attempt to turn an array list into a string that is comma separated, such as
[email protected],[email protected],[email protected],[email protected]
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-
Upvotes: 1
Views: 7023
Reputation: 13642
Guessing from the name of your variable (arrayList
), you've got List<string[]>
or an equivalent type there.
The issue here is that you're calling ToString()
on the array.
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
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>
. 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.
EDIT 2: If your ArrayList
actually contains System.Web.UI.WebControls.ListItem
s like you mentioned in your comment: arrayList.AddRange(ListBox.Items);
, then you'll need to use this instead:
Response.Write(string.Join(", ", arrayList.OfType<ListItem>()));
Upvotes: 6
Reputation: 1458
most of the answers are already there, still posting a complete - working snippet
string[] emailListOne = { "[email protected]", "[email protected]", "[email protected]", "[email protected]" };
string[] emailListTwo = { "[email protected]", "[email protected]", "[email protected]", "[email protected]" };
string[] emailListThree = { "[email protected]", "[email protected]", "[email protected]", "[email protected]" };
string[] emailListFour = { "[email protected]", "[email protected]", "[email protected]", "[email protected]" };
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());
Upvotes: -1
Reputation: 4572
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);
Upvotes: -1
Reputation: 52185
The second parameter for String.Join
needs to be an IEnumerable
. Replace MailingList.ToString()
with arrayList
and it should work.
Upvotes: 2