簡體   English   中英

如何識別列表 c# 中的不匹配值

[英]How to identified unmatched value in list c#

我這里有一個來自服務器的字符串 output,我想匹配我的 output 應該在我給定 RangeValueList 的范圍內,任何超出范圍的值,我的程序打印不匹配並且值匹配列表然后它將打印持續時間匹配,但我當前的代碼總是向我顯示持續時間匹配的消息,無論值是什么,我的代碼不起作用的任何線索謝謝

這是值

1.temp_initial = 00:02:34.964
2.temp_current = 00:02:36.972
                                                                        

**

            TimeSpan temp_initial = TimeSpan.Parse(_InitialTonKopfPosition);
            TimeSpan temp_current = TimeSpan.Parse(_CurrentTonKopfPosition);
            TimeSpan result = temp_current.Subtract(temp_initial);
            
            string output = result.ToString("g");
            Report.Info("Data", string.Format("Audio is played for '{0}' seconds", output));
            
            List<string> RangeValueList = new List<string>();
            RangeValueList.Add("0:00:01,997");
            RangeValueList.Add("0:00:01,998");
            RangeValueList.Add("0:00:01,999");
            RangeValueList.Add("0:00:02,000");
            RangeValueList.Add("0:00:02,001");
            RangeValueList.Add("0:00:02,002");
            RangeValueList.Add("0:00:02,003");
            RangeValueList.Add("0:00:02,004");
            RangeValueList.Add("0:00:02,005");
            RangeValueList.Add("0:00:02,006");
            RangeValueList.Add("0:00:02,007");
            RangeValueList.Add("0:00:02,008");
            RangeValueList.Add("0:00:02,009");
            //'0:00:02,008' 
                             
            int compareResult =0 ;
            foreach (string current in RangeValueList)
            {
                compareResult = String.Compare(current,output,StringComparison.CurrentCulture);
            }
            if(compareResult == 1)
                {
                    Report.Success("duration matched");
                }
                else
                {
                    Report.Failure("Not matched");
                }
            
           
            
        }
´´´
  1. 我認為這是因為您傳遞了g格式說明符,該說明符使用特定於文化的小數分隔符,可能是. 而不是,在您的系統中。 您可能想改用自定義格式字符串。
  2. 如果要檢查字符串值是否屬於某個字符串集合,我認為HashSet<string>是處理它的最佳 class 。
  3. 可以簡化集合初始化。
TimeSpan temp_initial = TimeSpan.Parse("00:02:34.964");
TimeSpan temp_current = TimeSpan.Parse("00:02:36.972");
TimeSpan result = temp_current.Subtract(temp_initial);

string output = result.ToString(@"h\:mm\:ss\,ffff");
Report.Info("Data", string.Format("Audio is played for '{0}' seconds", output));

var RangeValueList = new HashSet<string>
{
    "0:00:01,9970",
    "0:00:01,9980",
    "0:00:01,9990",
    "0:00:02,0000",
    "0:00:02,0010",
    "0:00:02,0020",
    "0:00:02,0030",
    "0:00:02,0040",
    "0:00:02,0050",
    "0:00:02,0060",
    "0:00:02,0070",
    "0:00:02,0080",
    "0:00:02,0090"
};

if (RangeValueList.Contains(output)) Report.Success("duration matched");            
else Report.Failure("Not matched");            

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM