如何使用字符串创建矩阵?

时间:2015-11-11 02:17:05

标签: c#

例如,如果我有{[1 0 3; 2 0 3; 3 0 3]},我怎么能让它看起来像

1 0 3

2 0 3

3 0 3

我知道我必须使用分隔符将1 0 3与2 0 3分开。我可以使用字符串的大小作为我的行吗?如果我能得到一些很棒的帮助。

1 个答案:

答案 0 :(得分:0)

如果要在控制台窗口中打印它们,请执行以下操作:

string str = "1 0 3; 2 0 3; 3 0 3";
string[] split = str.Split(';');

foreach (string s in split) //OR for (int i = 0; i < split.Length; i++)
{
    Console.WriteLine(s);
}

如果{}[]实际上是您不想要的字符,那么只需String.TrimString.Remove

当您执行str.Split(';')时,1 0 3存储在split[0]2 0 3 split[1]中,依此类推。当然,split[0][0]1split[0][1]0。考虑到这一点,您应该知道如何处理这些数组。

编辑:我想这就是你想要的:

for (int i = 0; i < split.Length; i++)
{
    for (int j = 0; j < split[i].Length; j++)
    {
        Console.Write(split[i][j]);
    }
    Console.WriteLine();
}