[英]How to make a textbox case insensitive
Yes, I realize that there are many things on this, but they aren't working for me. 是的,我意识到这有很多事情,但是它们对我没有用。
if (textBox1 != null)
{
string text = textBox1.Text;
foreach (string s in apple)
{
if (s.Contains(text))
{
listBox1.Items.Add(s);
}
}
}
In the listbox, I have: "Bob" and "Joe". 在列表框中,我有:“鲍勃”和“乔”。 The textbox searches for names but if I put in "joe", then it doesn't show joe's name, however, if I put "Joe", it shows the name. 文本框会搜索名称,但是如果我输入“ joe”,则不会显示joe的名称,但是,如果我输入“ Joe”,则会显示名称。
对所有人尝试ToLower()
:
if (s.ToLower().Contains(text.ToLower()))
You can use the string method ToLower()
if you want all letter lower or ToUpper()
if you want all letter Upper 如果要使所有字母都低,则可以使用字符串方法ToLower()
如果要使所有字母都高的话,可以使用ToUpper()
Ex: 例如:
if(txt!=null)
{
string text=txt.Text.ToLower();
foreach(string s in apple)
if(s.ToLOwer().Equals("YourString")
lst.Items.Add(s);
}
Unfortunately, the String.Contains
method does not have an overload that takes a StringComparison
argument to allow case-insensitive comparisons. 不幸的是, String.Contains
方法没有重载,该重载采用StringComparison
参数来允许不区分大小写的比较。 However, you can use the String.IndexOf
method instead. 但是,可以改用String.IndexOf
方法。
if (s.IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0)
Use String.IndexOf which gives you overload for StringComparison instead - 使用String.IndexOf可以给StringComparison重载-
if(s.IndexOf(text, StringComparison.InvariantCultureIgnoreCase) != -1)
{
}
Add this as an extension method to your project which i feel should already be there - 将它作为扩展方法添加到您的项目中,我觉得应该已经存在了-
public static class StringExtensions
{
public static bool Contains(this string value, string valueToCheck,
StringComparison comparisonType)
{
return value.IndexOf(valueToCheck, comparisonType) != -1;
}
}
Now you can use it like this from your method - 现在,您可以在您的方法中像这样使用它了-
if (s.Contains(text, StringComparison.InvariantCultureIgnoreCase))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.