简体   繁体   中英

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

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.

String s = "1:2;1:3;1:4";
String[][] f = s.Split( ';' ).Select( t => t.Split( ':' ) ).ToArray();
MyString.Split(';').Select(s => s.Split(':'))

That will give you an IEnumerable<IEnumerable<string>> . You can call .ToArray() if you really want to, but it's rarely worth it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM