// incoming single-string matrix:
String input = @"1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9";
// processing:
String[][] result = input
// Divide in to rows by \n or \r (but remove empty entries)
.Split(new[]{ '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
// no divide each row into columns based on spaces
.Select(x => x.Split(new[]{ ' ' }, StringSplitOptions.RemoveEmptyEntries))
// case from IEnumerable<String[]> to String[][]
.ToArray();
结果:
String[][] result = new string[]{
new string[]{ "1","2","3","4","5" },
new string[]{ "2","3","4","5","6" },
new string[]{ "3","4","5","6","7" },
new string[]{ "4","5","6","7","8" },
new string[]{ "5","6","7","8","9" }
};
什么语言这(将其添加到标签)是什么?你也只是阅读一行。 – Njol
也许我已经远离控制台应用太久了,但是如何接受'Console.ReadLine'中的'\ n'?读取是否在'\ n'上终止? –
@BradChristie - 在一个循环中接受多个'Console.ReadLine()' – Jamiec