简体   繁体   中英

c# convert string array to 2 dimensional char array

I need to convert a string array into a 2 dimensional char array

Example: My string array looks like

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

I want to convert it into a char[,] something like this (not sure about syntax)

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

what is the best way to achieve this?

You can do this:

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

Try like this,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM