Solved

asp.net - arraylist to comma delimited string

Posted on 2010-11-16
7
875 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
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 74

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

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 74

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now