How can I initialize a new string array from the values contained in a char array?
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();
精彩评论