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.
对所有人尝试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
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. However, you can use the String.IndexOf
method instead.
if (s.IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0)
Use String.IndexOf which gives you overload for StringComparison instead -
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.