我想把所有不包含单词“Time”的单词放在组合框中。
我试过了:
foreach (string stt in LTypes)
{
//if the stat name does not contains TIME
//Only then we add it to the combobox.
if (!stt.Contains("Time"))
{
tcomboBox1.Items.Add(stt);
}
}
但以上不起作用。 通过不起作用我的意思是包含“时间”的单词也被插入组合框中。
我哪里出错了?
答案 0 :(得分:3)
可能是套管问题。
试试这个:
if (!stt.ToUpperInvariant().Contains("TIME"))
无论如何都可以。
答案 1 :(得分:2)
对于我们从您的代码和您的问题描述中可以看到的内容,匹配失败(由于不同情况)或您的组合框一次又一次地填充。
试试这个:
//remember to clear your combobox if it is already populated!
tcomboBox1.Items.Clear();
foreach (string stt in LTypes)
{
//setting string to lower/upper case will let you match any occurence of your word
if (!stt.ToLowerCase().Contains("time"))
{
tcomboBox1.Items.Add(stt);
}
}
答案 2 :(得分:1)
您可以将IndexOf
与StringComparision.OrdinalIngoreCase
if (!(stt.IndexOf("Time", StringComparision.OrdinalIngoreCase) >=0))