forum

Home / DeveloperSection / Forums / How can I divide a set of strings into their constituent characters in C#?

How can I divide a set of strings into their constituent characters in C#?

Anonymous User 2341 06-Jun-2013
Hi Expert,

What is the best way to separate the individual characters in an array of strings strArr into an array of those characters charArr, as depicted below?

string[] strArr = { "123", "456", "789" };
char[] chrArr = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
This is what I am currently doing, but I do not think that it is very elegant:
int characterCount = 0;
for (int i = 0; i < strArr.Length; i++)
{
    characterCount += strArr[i].Length;
}
int indexCount = 0;
char[] chrArr = new char[characterCount];
for (int i = 0; i < strArr.Length; i++)
{
    for (int j = 0; j < strArr[i].Length; j++)
    {
        chrArr[indexCount] = strArr[i][j];
        indexCount++;
    }
}

Please help,
Thanks in advance. 

c# c# 
Updated on 06-Jun-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By