Let's say I have such a string: [a b c d e f]
. I would like to extract those characters and create a char array. Here is how I did it. Seems to be working:
var test = GetThemOut("[a b c d e f]");
// ...
public static char[] GetThemOut(string array)
{
return array
.Trim('[', ']')
.Split(' ')
.Select(x => x[0])
.ToArray();
}
Is there a simpler and a cleaner way to accomplish this? Well, it seems for me that my solution is really stupid and the language could provide a better one.
Still learning the language, trying to grasp tricks. Thanks.
EDIT
Alternative, as suggested:
public static char[] GetThemOut(string array)
{
return array
.Trim('[', ']')
.Replace(" ", "")
.ToCharArray();
}
String.ToCharArray
is what you're looking for. Use the MSDN! Also there's context sensitive help in VS that should have clued you in on it.array.Except(new[]{'[',']',' ']}.ToArray()