September 3, 2008

Some cool string manipulation in C#

Filed under: C# 3.0 — @ 2:04 am

Today I was writing a simple code which would readin a commandline parameters like

name param1=value1 param2=value2 param3=value

from the values it is very obivious the frst parameter is a individual name and others are named parameters with associated values. If we could do it in our normal mode, we would first assign the first parameter to the unique name field and then we would split each following arguments and put it in a array or arraylist or some sort.

Since it is key value pair the best bet would be Dictionary or Hash and I prefer Dictionary. For some reason I never came across a situation I had to use hash yet. Anyway, so I was going to write the split code and method and then I realised I could do the split and assign it o an arry in single statement and here is how I did it.

string[][] pharsed = argument.Skip(1).Select(s => s.split(‘=’)).ToArray();

IDictionary<string, string> dictionary = new Dictionary<string, string>();

foreach(string[] vaue in pharsed)


     dictionary.add(vaue[0], vaue[1]);


Thats it, what I liked was the ease to skip and split on the fly. I haven’t figured out, how to create Dictionary on the fly in that single statement. Once I find out how to do it, I will update it in here for my notes…

Have fun.

Powered by Qumana

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress