【问题标题】:How can I make a filter case insensitive?如何使过滤器不区分大小写?
【发布时间】:2013-10-24 03:45:18
【问题描述】:

我正在为学校做一个 ITP 项目。在这个项目中,我这样做是为了当我将一个单词添加到列表框中时,有一个过滤器可以在列表框中搜索该单词,如果匹配为假,则将该单词添加到列表中。但是这个过滤器不区分大小写,这意味着即使有奥迪它也会添加单词 audi,但是因为第一个字母是大写的,所以过滤器不会检测到这一点。这个位的代码是

private void btnAddWord_Click(object sender, EventArgs e)
    {
        if (this.lbxUnsortedList.Items.Contains(this.tbxAddWord.Text) == false)
        {
            //if the textbox is empty
            if (tbxAddWord.Text == "")
            {
                MessageBox.Show("You have entered no value in the textbox.");
                tbxAddWord.Focus();
            }
            //if the number of items in the listbox is greater than 29
            if (lbxUnsortedList.Items.Count > 29)
            {
                MessageBox.Show("You have exceeded the maximum number of values in the list.");
                tbxAddWord.Text = "";
            }

            //if the number of items in the listbox is less than 29
            else
            {

                //add word to the listbox
                this.lbxUnsortedList.Items.Add(this.tbxAddWord.Text);
                //update tbxListBoxCount
                tbxListboxCount.Text = lbxUnsortedList.Items.Count.ToString();
                //onclick, conduct the bubble sort
                bool swapped;
                string temp;
                do
                {
                    swapped = false;
                    for (int i = 0; i < lbxUnsortedList.Items.Count - 1; i++)
                    {
                        int result = lbxUnsortedList.Items[i].ToString().CompareTo(lbxUnsortedList.Items[i + 1]);
                        if (result > 0)
                        {
                            temp = lbxUnsortedList.Items[i].ToString();
                            lbxUnsortedList.Items[i] = lbxUnsortedList.Items[i + 1];
                            lbxUnsortedList.Items[i + 1] = temp;
                            swapped = true;
                        }
                    }
                } while (swapped == true);
                tbxAddWord.Text = "";
            }
        }
        if (this.lbxUnsortedList.Items.Contains(this.tbxAddWord.Text) == true)
        {
            MessageBox.Show("The word that you have added is already on the list");
            tbxAddWord.Text = "";
            tbxAddWord.Focus();
        }

    }

我想知道如何使这个大小写不敏感,这样即使第一个字母是大写的,过滤器也能识别奥迪。

【问题讨论】:

    标签: c# filter listbox uppercase


    【解决方案1】:

    我只是建议你这个条件,它不是最佳的,但它可以按照你想要的方式工作:

            bool contains = false;
    
            for (int i = 0; i < lbxUnsortedList.Items.Count; i++)
            {
                if (lbxUnsortedList.Items[i].ToString().ToLower() == this.tbxAddWord.Text.ToString().ToLower())
                {
                    contains = true;
                }
    
            }
    
            if (!contains)
            {
                //your code
            }
            else
            {
                MessageBox.Show("The word that you have added is already on the list");
                tbxAddWord.Text = "";
                tbxAddWord.Focus();
    
            }
    

    【讨论】:

    • 这段代码完美运行。但是在弹出错误信息后,列表中增加了一个空格。有什么办法可以阻止吗?
    • 呵呵,没问题。太好了,我可以帮忙。
    【解决方案2】:

    试试这个

      public bool checkItemExist(string itemToCheck)
        {
             return lbxUnsortedList.Items.Cast<string>.Where(a=>a.ToLower().Equals(tbxAddWord.Text.ToLower()).Count > 0;
        }
    

    【讨论】:

      【解决方案3】:

      这个link 应该给你你正在寻找的东西。帖子的某些部分说

      culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase)

      另外,您可以尝试以小写形式存储单词

      this.lbxUnsortedList.Items.Add(this.tbxAddWord.Text.ToLower());

      然后用同样的方法搜索字符串

      this.lbxUnsortedList.Items.Contains(this.tbxAddWord.Text.ToLower()) == true

      但如果您使用 'lbxUnsortedList' 进行显示,那么使用字典作为普通单词的键和小写的值也可能会有所帮助。

      此外,如果您允许我扔其他东西,请使用 string.IsNullOrEmpty(tbxAddWord.Text) 而不是 tbxAddWord.Text == ""。你也可以使用tbxAddWord.Text = string.Empty;

      【讨论】:

        【解决方案4】:

        在比较两个字符串值之前,必须将字符串转换为大写或小写。

        遍历列表框,lbxUnsortedList 以从列表中获取项目。并将每个字符串与来自 TextBox 的输入字符串进行比较,tbxAddWord

        for (int i = 0; i < lbxUnsortedList.Items.Count; i++)
        {
            if (lbxUnsortedList.Items[i].ToString().ToLower() == this.tbxAddWord.Text.ToString().ToLower())
            {
                 //Your Code
            }
        }
        

        【讨论】:

          【解决方案5】:

          对我来说是这样的

          var query = _repository.Get(e => e.IsDeleteUser != true, null, "Role");

                  if (!string.IsNullOrEmpty(filter.UserName))
                  {
                      query = query.Where(e => e.UserName.**ToLower()**.Contains(filter.UserName**.ToLower()**)).ToList();
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-08-07
            • 2022-12-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多