c#将字符串数组转换为2维char数组

时间:2016-07-04 10:52:33

标签: c# arrays string char

我需要将字符串数组转换为2维字符数组

示例:我的字符串数组看起来像

string[] months = {"January", "February", "March"....};

我想将其转换为char[,]这样的东西(不确定语法)

char[][] = {
    {'J','a','n','u','a','r','y'},
    {'F','e','b','r','u','a','r','y'},
    {'M','a','r','c','h'}
};

实现这一目标的最佳方法是什么?

2 个答案:

答案 0 :(得分:5)

你可以这样做:

string[] months = { "January", "February", "March" };
char[][] result = months.Select(item => item.ToArray()).ToArray();

答案 1 :(得分:0)

试试这个,

string[] months = {"January", "February", "March"};

char[][] jaggedOfChar =new char[3][];

for (int i = 0; i < months.Length; i++)
{
    char[] s = months[i].ToCharArray();
    jaggedOfChar[i] = s;
}