简体   繁体   English

如何使过滤器不区分大小写?

[英]How can I make a filter case insensitive?

I was doing a ITP project for school.我正在为学校做一个 ITP 项目。 In this project, i made it so that when i add a word into a listbox, there is a filter which searches for the word in the listbox and if the match is false, adds the word into the list.在这个项目中,我这样做是为了当我将一个词添加到列表框中时,有一个过滤器会在列表框中搜索该词,如果匹配为假,则将该词添加到列表中。 But this filter is not case insensitive meaning it will add the word audi even though there is a Audi, but because the first letter is upper case, the filter does not detects this.但是这个过滤器不是不区分大小写的,这意味着即使有奥迪它也会添加单词奥迪,但因为第一个字母是大写的,过滤器不会检测到这一点。 the code for this bit is这个位的代码是

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();
        }

    }

I want to know how i can make this case insensitive so that the filter will pickup Audi even though the first letter is uppercase.我想知道如何使这种大小写不敏感,以便即使第一个字母是大写的,过滤器也会选择 Audi。

Try this试试这个

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

I would simply suggest you this condition, it's not optimal but it works the way you want :我只是建议你这个条件,它不是最佳的,但它以你想要的方式工作:

        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();

        }

This link should give you what you are looking for.这个链接应该给你你正在寻找的东西。 Some part of post says帖子的某些部分说

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

Also as an alternative, you can try to store words in the lower case作为替代方案,您可以尝试以小写形式存储单词

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

Then search for the string in same way然后以同样的方式搜索字符串

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

But in case you are using 'lbxUnsortedList' for display purpose then using a dictionary with key as normal words and value in lower case may also be helpful.但是,如果您使用 'lbxUnsortedList' 进行显示,那么使用带有键作为普通单词和小写值的字典也可能会有所帮助。

Also if you allow me to throw something else then use string.IsNullOrEmpty(tbxAddWord.Text) instead of tbxAddWord.Text == "" .另外,如果您允许我抛出其他内容,请使用string.IsNullOrEmpty(tbxAddWord.Text)而不是tbxAddWord.Text == "" Also you may want to use tbxAddWord.Text = string.Empty;你也可能想使用tbxAddWord.Text = string.Empty; . .

Before you compare the two string values, you have to convert the strings to upper case or lower case.在比较两个字符串值之前,您必须将字符串转换为大写或小写。

Loop through the list box, lbxUnsortedList to get the items from the list.循环遍历列表框lbxUnsortedList以从列表中获取项目。 And compare each string with input string from TextBox, tbxAddWord .并将每个字符串与来自 TextBox 的输入字符串tbxAddWord

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

for me it is works like this对我来说是这样的

var query = _repository.Get(e => e.IsDeleteUser != true, null, "Role"); 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();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM