?
Solved

asp.net - arraylist to comma delimited string

Posted on 2010-11-16
7
Medium Priority
?
888 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 200 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 200 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 1600 total points
ID: 34148487
What about this:
strString = "'" + String.Join("', '", arrArrayList.ToArray) + "'"

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1600 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

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!

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.…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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