Alex Rivera | Logout

Neat code to convert bool[] -> "false, true, true, false"

Asked 2010-02-16T12:56:23.880
10

How would you convert an array of booleans to a string like "false, true, true, false" - using as few lines of code as possible?

Python allows me to use the following (very nice and clean):

", ".join(map(str, [False, True, True, False]))

In C#, string.Join only allows me to join an array of strings.

So what is a short way to do the same in C#?

Edit
Report

3 Answers

4

How about:

String.Join(", ", new List<Boolean>() { true, false, false, true }.ConvertAll(x => x.ToString()).ToArray())
answered 2010-02-16T12:59:14.790
4
arrayOfBools.Select(x => x.ToString()).Aggregate((x, y) => x + ", " + y)
answered 2010-09-25T21:46:59.947
1

Something along the lines of

var s = from item in blnArray select item.Tostring();

Then use the enumerable s to populate the string array?

Not actually tested this - this is just how I might approach it if I were asked to look at it...

answered 2010-02-16T13:02:34.863

Your Answer