Solved

asp.net - arraylist to comma delimited string

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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