Monday, March 25, 2013

[C#] Split the Split!

I've been doing some C# stuff when I came across some quite but not so complicated matter: splitting a just split string. In other words, I want to split a string into an array then split again elements on it giving me a 2-dimensional array.

So here's the solution I found:

String input = "1=2;3=4;5=6";

String myArray1[] = input.Split(';');
String myArray2[][] = input.Split(';').Select(s => s.Split('=')).ToArray();

myArray1 will give {1=2, 3=4, 5=6} which is a 1-dimension array while myArray2 will give {1,2},{3,4},{5,6} which is a 2-dimension array.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...