开发者

How can I initialize a new string array from the values contained in a char array?

开发者 https://www.devze.com 2022-12-10 01:17 出处:网络
I would like to initialize a new string array from the values contained in a char array. Is this possible without using a list?

I would like to initialize a new string array from the values contained in a char array.

Is this possible without using a list?

This is what I have so far:

char[] arrChars = {'a', 'b', 'c'};
string[] arrStrings = new string[](arrChars);开发者_运维技巧


string[] arrStrings = Array.ConvertAll(arrChars, c => c.ToString());


Why not use a for loop for your initialization? Or, if that's too many LOC, you could just use Linq:

string[] arrStrings = arrChars.Select(c => c.ToString()).ToArray();


.NET 2:

char[] arrChars = {'a', 'b', 'c'};
string[] arrStrings = Array.ConvertAll<char, string>(arrChars, delegate(char c)
{
    return c.ToString();
});


using LINQ......

char[] arrChars = {'a', 'b', 'c'};
string[] arrStrings =( from c in arrChars select "" + c).ToArray();


string[] arrStrings = arrChars.Select( c => new String(new []{c}) ).ToArray();

or

string[] arrStrings = arrChars.Select( c => c.ToString() ).ToArray();
0

精彩评论

暂无评论...
验证码 换一张
取 消