I would like to concatenate strings (from an array) based on an input int value.
For example:
int Input = 3;
string[] Items = {"A", "B", "C", "D"};
// output = "A, B"
// example 2: input = 8
// output = "D"
The first element of the array (0) should be associated with the least significant bit of the input value. Values in the array are concatenated with a string such as “,” or “+”.
What’s a good way to do this?
So, you want to treat your input value as a bitmask?
This should work:
Basically what I’m doing is converting your number into a BitArray, which will allow me to easily tell what bits are set and not set in your number. I’m then using a little Linq to filter the Items array by index, based on whether the corresponding index of the BitArray is set.
Understand that this will only work if the array has less than sizeof(int)*8 (32) elements. Beyond that, the BitArray won’t have indexes to Get() unless you specify additional integer values that can be chained together in the int[] array passed to the BitArray constructor.