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#?
var array = new[] { true, false, false };
var result = string.Join(", ", array.Select(b => b.ToString()).ToArray());
Console.WriteLine(result);
How about:
String.Join(", ", new List<Boolean>() { true, false, false, true }.ConvertAll(x => x.ToString()).ToArray())
arrayOfBools.Select(x => x.ToString()).Aggregate((x, y) => x + ", " + y)
If you are using .NET 4, the following line is enough, because String.Join<T>
internally calls the ToString()
-method for every item.
Console.WriteLine(string.Join(", ", new[] { false, true, true, false }));
>>>> False, True, True, False
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