[英]better way to multiple test what contains a string
I'm currently working with culture Info and need to translate some words.我目前正在与文化信息合作,需要翻译一些单词。 For the days, i use this :
这几天,我用这个:
--> input : an array DayDetails : [MO,TU, WE, TH, FR]
or [MO, WE,TH,FR,SA]
... --> 输入 :
array DayDetails : [MO,TU, WE, TH, FR]
或[MO, WE,TH,FR,SA]
...
if ( daysDetails.Length == 7 )
{
return langSpec.GetLocalMessage( "every" ) + " " + langSpec.GetLocalMessage( "Days" );
}
else if( daysDetails.Length == 5 && daysDetails.Contains( "MO" ) && daysDetails.Contains( "TU" ) && daysDetails.Contains( "WE" ) && daysDetails.Contains( "TH" ) && daysDetails.Contains( "FR" ) )
{
return langSpec.GetLocalMessage( "workingDays" );
}
output : a string
with : Monday, Thuesday, Wednesday输出:一个
string
:星期一、星期四、星期三
if there's 5 days and it's the working days i want : a string
with : working days如果有 5 天,这是我想要的工作日:一个
string
:工作日
if there's 7 days i want : a string
with "every days"如果我想要 7 天:一个带有“每一天”的
string
the thing is, i don't like how my second if looks but i have to test, in case of 5 days, that the days are the working days to proper translate it.问题是,我不喜欢我的第二个 if 看起来如何,但我必须测试,在 5 天的情况下,这些天是正确翻译它的工作日。
Is there a better way to write this, other than multiple && ?除了多个 && 之外,有没有更好的方法来写这个?
Sorry for my bad english speaking, hope you'll understand.抱歉我的英语不好,希望你能理解。
EDIT : DayDetails can be also [MO,TH], or [MO] or [FR,WE,TH,TU,MO] since it's a response i got from somewhere else and i can't master the input.编辑:DayDetails 也可以是 [MO,TH] 或 [MO] 或 [FR,WE,TH,TU,MO],因为它是我从其他地方得到的响应,我无法掌握输入。
EDIT 2 : i checked Compare two List<T> objects for equality, ignoring order since multiple people suggested it, but it does not really answer to my question.编辑 2:我检查了比较两个 List<T> 对象是否相等,忽略顺序,因为多人建议它,但它并没有真正回答我的问题。 the answer given is way more complicated that my initial "if" with 5 statements.
给出的答案比我最初的带有 5 个语句的“if”要复杂得多。
Make an array and use All:创建一个数组并使用 All:
private static readonly List<string> workdays = new List<string>() { "MO", "TU", "WE", "TH", "FR" };
private static bool isWorkday(string[] daysDetails) => workdays.All(d => daysDetails.Contains(d));
You can use Enumerable.SequenceEqual
as follows:您可以使用
Enumerable.SequenceEqual
如下:
string[] dayDetails = new [] { "MO", "TU", "WE", "TH", "FR" };
if (daysDetails.Length == 7)
{
return langSpec.GetLocalMessage( "every" ) + " " + langSpec.GetLocalMessage( "Days" );
}
else if (Enumerable.SequenceEqual(dayDetails, new [] { "MO", "TU", "WE", "TH", "FR" }))
{
return langSpec.GetLocalMessage( "workingDays" );
}
Assuming dayDetails
will be in the correct order.假设
dayDetails
将按正确顺序排列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.