如何使用C#转换2维数组字符串[,]字符串 [英] How to use C# convert 2 dimensional array string[,] to string

查看:250
本文介绍了如何使用C#转换2维数组字符串[,]字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个二维数组

string[,] temp = new string[,]{{1,2,3},{4,5,6},{7,8,9}};

提醒:

string[,] != string[][]

我要转换为...

I want to convert to ...

123

456

789

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

How to fast convert in this case ?

推荐答案

下面是2嵌套循环的方式:

Here is a way with 2 nested loops:

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();
}

如果你正在考虑,你可以把二维数组作为一维和绕过2路有一些技巧在这里:<一href=\"http://stackoverflow.com/questions/797354/how-to-copy-a-row-of-values-from-a-2d-array-into-a-1d-array\">How从二维数组复制值​​的一行到一维数组?,但为了能够使用这些技巧,你需要的char数组,数组不是字符串,因为你问。

If you are thinking you can treat the 2-d array as 1-d and bypass the 2 loops there are some tricks here: How to copy a row of values from a 2D array into a 1D array? , but to be able to use these tricks you would need array of char, not array of string as you have asked.

这篇关于如何使用C#转换2维数组字符串[,]字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆