Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#将二维数组字符串[,]转换为字符串_C#_Arrays_String_Dimensional - Fatal编程技术网

如何使用C#将二维数组字符串[,]转换为字符串

如何使用C#将二维数组字符串[,]转换为字符串,c#,arrays,string,dimensional,C#,Arrays,String,Dimensional,我有二维阵列 var temp = new string[,] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } }; 提醒: string[,] != string[][] 我想转换成 123 456 789 在这种情况下如何快速转换?以下是一种使用两个嵌套循环的方法: var temp = new string[,]{{"1","2","3"},{"4","5","6"}}; var output = new str

我有二维阵列

var temp = new string[,] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } };
提醒:

string[,] != string[][]
我想转换成

123

456

789

在这种情况下如何快速转换?

以下是一种使用两个嵌套循环的方法:

var temp = new string[,]{{"1","2","3"},{"4","5","6"}};
var output = new string[temp.GetUpperBound(0)+1];
for (int i = 0; i<=temp.GetUpperBound(0); i++)
{
    var sb = new StringBuilder(temp.GetUpperBound(1)+1);
    for (int j = 0; j<=temp.GetUpperBound(1); j++)
        sb.Append(temp[i,j]);
    output[i] = sb.ToString();
}
var-temp=新字符串[,]{{“1”、“2”、“3”}、{“4”、“5”、“6”};
var输出=新字符串[temp.GetUpperBound(0)+1];

对于(int i=0;i对于单个代码行,您可以使用:

var temp = new string[,] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } };
var result = string.Join("\r\n\r\n", 
    temp.OfType<string>()
    .Select((str, idx) => new {index = idx, value = str})
    .GroupBy(a => a.index/(temp.GetUpperBound(0) + 1))
    .Select(gr => gr.Select(n => n.value).ToArray())
    .Select(a => string.Join("", a.SelectMany(x => x)))
    .ToArray());
要仅连接多维数组,可以使用:

string[,] multidimensional2d = { { "1", "2", "3" }, { "4", "5", "6" } };
string[,,] multidimensional3d = { { { "1", "2" }, { "3", "4" } }, { { "5", "6" }, { null, null } } };
string multidimensional2dConcatenate = string.Join(", ", multidimensional2d.OfType<string>());
string multidimensional3dConcatenate = string.Join(", ", multidimensional3d.OfType<string>());
私有静态字符串ObjectToString(IList消息)
{
StringBuilder=新的StringBuilder();
foreach(消息中的var项)
{
如果(builder.Length>0)
生成器。追加(“”);
如果(项目为IList)
Append(ObjectToString((IList)项));
其他的
附加(项目);
}
返回builder.ToString();
}

oyu做了哪些努力?您尝试过哪些代码?
string[,] multidimensional2d = { { "1", "2", "3" }, { "4", "5", "6" } };
string[,,] multidimensional3d = { { { "1", "2" }, { "3", "4" } }, { { "5", "6" }, { null, null } } };
string multidimensional2dConcatenate = string.Join(", ", multidimensional2d.OfType<string>());
string multidimensional3dConcatenate = string.Join(", ", multidimensional3d.OfType<string>());
string[][][] array3d = { new[] { new[] { "1", "2" } }, new[] { new[] { "3", "4" } }, new[] { new[] { "5", "6" } }, null };
string[][][] jagged3d = { new[] { new[] { "1", "2" }, new[] { "3" } }, new[] { new[] { "4" }, new[] { "5" } }, new[] { new[] { "6" }, null }, null };

string array3dConcatenate = string.Join("\r\n\r\n", array3d.Where(x => x != null).SelectMany(x => x).Where(x => x != null).Select(a => string.Join("", a.SelectMany(x => x))));
string jagged3dConcatenate = string.Join("\r\n\r\n", jagged3d.Where(x => x != null).SelectMany(x => x).Where(x => x != null).Select(a => string.Join("", a.SelectMany(x => x))));
private static string ObjectToString(IList<object> messages)
    {
        StringBuilder builder = new StringBuilder();
        foreach (var item in messages)
        {

            if (builder.Length > 0)
                builder.Append(" ");
            if (item is IList<object>)
                builder.Append(ObjectToString((IList<object>)item));
            else
                builder.Append(item);

        }

        return builder.ToString();
    }