Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# loopless way to split string into multidimensional array or jagged array

Tags:

c#

linq

How do I split a string into a multidimensional array or a jagged array without looping? I saw a code snippet where someone was doing so with a file stream and I can't figure out how to make it work for my string. My string is similar to 1:2;3:1;4:1 and can be split into ID:Qty. Here is the code I saw:

string[][] lines = File.ReadAllLines(path)
    .Select(line => line.Split(',').ToArray()).ToArray();   

Thanks in advance.

like image 620
Praesagus Avatar asked Dec 23 '22 08:12

Praesagus


1 Answers

String s = "1:2;1:3;1:4";
String[][] f = s.Split( ';' ).Select( t => t.Split( ':' ) ).ToArray();
like image 127
Philip Davis Avatar answered Dec 24 '22 20:12

Philip Davis