I have an array that looks something like:
status[0] = true
status[1] = true
status[2] = false
status[3] = true
In reality it's larger but still less than 20. I need to convert this into "ABD". Where each true represents an ordered letter in the alphabet. Can anyone think of an easy really efficient way to do this?
My napkin says this might work...
StringBuilder sb = new StringBuilder();
for(int i = 0; i < status.Length; i++)
{
if(status[i])
{
sb.Append((char)('A' + i));
}
}
string result = sb.ToString();
string input = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string result = new String(input.ToCharArray()
.Take(status.Length)
.Where((c, i) => status[i]).ToArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With