Sunday, March 31, 2013

Convert a Comma Delimited String to an ArrayList in C#


In the previous post i had shown u how to Convert comma delimited string to Dictionary
Now i will show how to Convert comma delimited string to string array, is easy as shown below...



string Val = "a,b,c,d,e";

string[] myStringArray = Val.Split(',');


in the  same way converting comma delimited string to arraylist is also easy in a single line as shown below....

//Method1
ArrayList myArrayList = new ArrayList();
myArrayList.AddRange(Val.Split(','));



//Method 2
ArrayList list = new ArrayList(Val.Split(','));
 
 
Out Put :


myArrayList[0] = a;
myArrayList[1] = b;
myArrayList[2] = c; 
myArrayList[3] = d;
myArrayList[4] = e;

Convert a Comma Delimited String to an Dictionary in C#

Here i will show how to Convert a Comma Delimited String to an Dictionary in C#
In the previous post i had shown u how to get all selected values from ListBox
continuing the above link.....



string Val = "a,b,c,d,e";

Dictionary<string, string> MyDict1 = new Dictionary<string, string>();

MyDict1 = Val.Split(',').ToDictionary(key => key.Trim(), value => value.Trim());

Out Put :

a -> a
b -> b
c -> c
d -> d
e -> e