Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Split String with Containing Parentheses into multi-dimensional array

Tags:

arrays

c#

I have the following String:

"(X,Y,Z),(A,B,C),(R,S,T)"

I want to split this into a multi-dimensional array:

arr[0] = [x,y,z]
arr[1] = [a,b,c]
arr[2] = [r,s,t]

so that:

arr[0][1] = y,  arr[0][2] = z, etc.

I can do it by stripping the first and last parens, splitting on "),(" and then looping through that array and doing another split. But I feel dirty, unpure, like a stripper (pun intended) in a backalley bar ... is there a cleaner way?

Maybe some LINQ to the rescue?

I'm using C#.

like image 513
OpenR Avatar asked Feb 24 '12 20:02

OpenR


1 Answers

string data = "(X,Y,Z),(A,B,C),(R,S,T)";

string[][] stringses = data.Trim('(', ')')
    .Split(new[] {"),("}, StringSplitOptions.RemoveEmptyEntries)
    .Select(chunk => chunk.Split(','))
    .ToArray();
like image 152
Ed Chapel Avatar answered Oct 18 '22 03:10

Ed Chapel