Solved

asp.net - arraylist to comma delimited string

Posted on 2010-11-16
7
882 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 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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