我有7个布尔值。基于它们的值,我需要构建一个字符串。最有效的方法是什么?
为了举例,我的布尔值是:
bool green = false;
bool red = true;
bool purple = true;
bool orange = false;
bool black = true;
bool blue = true;
bool brown = false;
每个布尔变量(理论上)都有一个与之关联的字符串。我想要生成的最后一个字符串取决于' true'值。
在这里,我想要生成的字符串是"redskins - ravens - raiders - texans"
。
与' false'相关联的字符串变量不会包含在最终字符串中。
我正在处理的实际问题是规模较大,大约有50个布尔值,我的要求有50个与之关联的字符串。
使用if-else
?
StringBuilder
方法更有效
我的变量名称不代表我需要输出的字符串。
答案 0 :(得分:8)
bool green = false;
bool red = true;
bool purple = true;
bool orange = false;
bool black = true;
bool blue = true;
bool brown = false;
var bools = new Dictionary<string, bool>
{
{ $"{nameof(green)}", green}
,{ $"{nameof(red)}", red}
,{ $"{nameof(purple)}", purple}
,{ $"{nameof(orange)}", orange}
,{ $"{nameof(black)}", black}
,{ $"{nameof(blue)}", blue}
,{ $"{nameof(brown)}", brown}
};
Console.Write(string.Join("-", bools.Where(b => b.Value).Select(b => b.Key)));
答案 1 :(得分:0)
我正在考虑使用字典进行配对。
static void Main(string[] args)
{
Dictionary<string, string> pair = new Dictionary<string, string>();
pair.Add("green", "green");
pair.Add("red", "redskiins");
pair.Add("purple", "ravens");
pair.Add("orange", "orange");
pair.Add("black", "raiders");
pair.Add("blue", "texans");
pair.Add("brown", "brown");
List<string> display = new List<string>();
bool green = false;
bool red = true;
bool purple = true;
bool orange = false;
bool black = true;
bool blue = true;
bool brown = false;
if (green)
display.Add(pair["green"]);
if (red)
display.Add(pair["red"]);
if (purple)
display.Add(pair["purple"]);
if (orange)
display.Add(pair["orange"]);
if (black)
display.Add(pair["black"]);
if (blue)
display.Add(pair["blue"]);
if (brown)
display.Add(pair["brown"]);
string finalStr = string.Join(" - ", display);
Console.WriteLine(finalStr);
Console.Read();
}
输出:
答案 2 :(得分:0)
var boolArray = GetLotsOfBools();
var strArray = GetLotsOfStrings();
return string.Join(" - ",
boolArray
.Select((@bool, x) => new {
Bool = @bool,
Str = strArray[x]
}).Where(pair => pair.Bool)
.Select(pair => pair.Str)
.ToArray()
);
答案 3 :(得分:0)
我会做这样的事情,使用索引到输入字符串(颜色)的字典,并使用元组作为值:
// Create your dictionary, indexed with the color, with a tuple as a value to describe
// boolean state and matching string fragment
var dictTeams = new Dictionary<string, Tuple<bool, string>>()
{
{ "green", new Tuple<bool, string>(false, "packers") },
{ "red", new Tuple<bool, string>(true, "redskins") },
{ "purple", new Tuple<bool, string>(true, "ravens") },
{ "orange", new Tuple<bool, string>(false, "bears") },
{ "black", new Tuple<bool, string>(true, "raiders") },
{ "blue", new Tuple<bool, string>(true, "texans") },
{ "brown", new Tuple<bool, string>(false, "browns") }
};
// You can either initialize your dictionary all at once or fill it in later on.
dictTeams.Add("white", new Tuple<bool, string>(true, "patriots"));
// Select the keys of the entries marked true and aggregate them into a string.
// Here you could also add a filter for only colors selected in another list by adding
// a constraint in the Where clause using the key in addition to the boolean value.
var outString = dictTeams.Where(c => c.Value.Item1)
.Select(c => c.Value.Item2)
.Aggregate((a, b) => a + " - " + b);
请注意,元组是不可变的,因此如果布尔状态需要在运行时更改,请为字典值创建两个字段类,这样您就可以执行以下操作:
dictTeams["green"].BoolState = false;
和
var outString = dictTeams.Where(c => c.Value.BoolState && lstUserColors.Contains(c.Key))
.Select(c => c.Value.StringFragment)
.Aggregate((a, b) => a + " - " + b);