Solved

asp.net - arraylist to comma delimited string

Posted on 2010-11-16
7
884 Views
Last Modified: 2012-06-27
I have an ArrayList with x number of items.
I would like to convert this ArrayList into a comma delimited string.
In addition, each item must be surrounded by single quotes.

This is what I have so far:

            strString = String.Join(",", arrArrayList.ToArray)

This gives me:

            strString = "item1, item2, item3, item4"


This is what I need:

            strString = "'item1', 'item2', 'item3', 'item4'"



Thank you.
0
Comment
Question by:Rick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 50 total points
ID: 34148459

unfortunately, i think you will have to address each string in the array

and then just build the string

str = "'" + str.Trim() + "'"

and then after that call the string join on the array.
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 50 total points
ID: 34148475
Here's a simple function to loop through each item in the array, appending each one to a StringBuilder.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			ArrayList list = new ArrayList();

			list.AddRange(new string[] { "One", "Two", "Three" });

			string combined = ArrayListToCommaDelimited(list);
			
			Console.WriteLine(combined);
			Console.ReadKey();
		}

		private static string ArrayListToCommaDelimited(ArrayList array)
		{
			StringBuilder commaString = new StringBuilder();
			for (int i = 0; i < array.Count - 1; i++)
				commaString.AppendFormat("'{0}',", array[i]);
			commaString.AppendFormat("'{0}'", array[array.Count - 1]);
			return commaString.ToString();
		}
	}
}

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 400 total points
ID: 34148487
What about this:
strString = "'" + String.Join("', '", arrArrayList.ToArray) + "'"

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 34148508
The eerie thing is I came up with this not 5 minutes prior to reading your question for one of my own projects...  spooky  ;)
0
 
LVL 13

Author Closing Comment

by:Rick
ID: 34148566
I almost had it.

strString = " String.Join("', '", arrArrayList.ToArray) + "'"

Thank you.


0
 
LVL 32

Expert Comment

by:Erick37
ID: 34148582
You could loop the array and quote the strings one at a time:
Dim a() As Object = arrArrayList.ToArray

		For i As Integer = 0 To a.Count - 1
			a(i) = String.Format("'{0}'", a(i))
		Next

		strString = String.Join(",", a)

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34148618
NP. Glad to help  :)
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question