2

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?

6
  • 4
    I'm afraid you'll need to flesh out your question a bit better. What value (true or false) become what letter? Commented May 4, 2011 at 23:30
  • @Jonathan I would assume 0 = 'A', 1 = 'B', ... but it would be nice for Beth to clarify Commented May 4, 2011 at 23:32
  • Pretty sure she means that the each index of the array is associated with the same index in the alpha bet. In her sample, 'true, true, false, true' = "ABD". Commented May 4, 2011 at 23:34
  • Sorry if I was not clear. Yes 0=A, 1=B etc. thanks Commented May 4, 2011 at 23:36
  • @Beth: If 0=A & 1=B, then what does "D" equal? I read your question the same way @Jon did. Commented May 4, 2011 at 23:49

5 Answers 5

7

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();
Sign up to request clarification or add additional context in comments.

2 Comments

As far as I can tell, this won't work. I get 656668. The reason is that you are not incrementing the value you cast to a char prior to casting it. You need to do something like sb.Append( (char) ( i + 'A' ) );
@svick - That's better. +1. Good job.
3
string input = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

string result = new String(input.ToCharArray()
                      .Take(status.Length)
                      .Where((c, i) => status[i]).ToArray());

3 Comments

I'm sorry but I just had to point out how ugly that seems to me. :-) And I would love to see some comparisons of performance.
@svick added Take(length) for that.
@Beth it should be status.Length. Fixed now :D
3

You can use Linq:

var status = new bool[] { true, true, false, true };

// alternative 1
var statusString = string.Concat(
    status.Select((val, index) => val ? (char?)('A' + index) : null)
          .Where(x => x != null));

// alternative 2
var statusString2 = string.Concat( 
    status.Select((val, index) => val ? (object)(char)('A' + index) : ""));

// alternative 3 (same as one, no boxing)
var statusString3 = string.Concat(
    status.Select((val, index) => val ? (char)('A' + index) : ' ')
          .Where(x => x != ' '));


// alternative 4 (no Linq, probably faster)
var statusString4 = new StringBuilder(status.Length);
for (int i = 0; i < status.Length; i++)
{
    if (status[i])
        statusString4.Append((char)('A' + i));
}

2 Comments

I think this is the best answer so far. +1
+1 Since this answer shows some LINQ methods, What about using Zip (they finally added this in .NET 4?) and Where over the input and enumeration of 'A'..'Z'?
2
String[] alphabet = new String[]  {"A", "B", ... "Z"};
String result = "";

for (int i = 0; i < status.Length; i++)
{
    if (status[i] == true) result += alphabet[i];
}

Basically, you can create an array of the letters of the alphabet and match the true values of your status array to the corresponding letter.

5 Comments

Since the letters are sequential, there is no need for the Alphabet array.
if (foo == true)!! 5 points from Gryffindor! (no need for == true)
@Jonathan: Very true. For simplicity, I created the alphabet array. If the letters ever need to change, it's easy to modify the array.
@Daniel: After many, many years of programming, I personally prefer the == true.
@Jonathan Wood - After many, many years of programming I hate ==true because it eventually morphs to !=true or !=false. Obviously personal preference.
0

Create a class like this:

class Bool2CharArrayConverter
{

    public static string ConvertArray(bool[] Input)
    {
        int asc = 65;
        StringBuilder response = StringBuilder();
        foreach (bool val in Input)
        {
            if (val)
            {
                response.Append( Convert.ToChar(asc));
            }
            asc++;
        }
        return response.ToString();
    }
}

which can be invoked like this:

     bool[] status = new bool[]{true,true,false,true};
     string output = Bool2CharArrayConverter.ConvertArray(status);
     Console.WriteLine(output); //output = ABD

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.