+ 2

Can Contains be use case insensitive

I would like to make a filter, that is searching case in sensitive items.Add("apple"); items.Add("apple banana "); items.Add("Apple Banana Lemon"); Using this list I am searching for "b", I would like to get 2 lines returned. Since contains is case sensitive, I only get 1 line. How to solve this ? https://code.sololearn.com/cJh0D4vMZwzw/?ref=app

7th Nov 2022, 7:14 PM
sneeze
sneeze - avatar
5 Answers
+ 5
Yes,exists i => i.Contains("b",StringComparison.OrdinalIgnoreCase) Also You can try : i => i.ToLower().Contains("b")
7th Nov 2022, 7:33 PM
Jayakrishna 🇼🇳
+ 3
Thank you. the first one, did it. The second one should work as wel, but is less elegant.
7th Nov 2022, 8:12 PM
sneeze
sneeze - avatar
+ 2
I think, by search, visual Studio only supports .Net framework. Not the .Net Core. It's avialable in core. "On .NET Core 2.1 and later versions: Call the Contains(String, StringComparison) overload instead." https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=net-7.0 so Use any alternatives like IndexOf method https://stackoverflow.com/questions/444798/case-insensitive-containsstring
8th Nov 2022, 11:01 AM
Jayakrishna 🇼🇳
+ 1
Unfortunately. On sololearn this works but in my visual studio i get the following error. CS1501 C# No overload for method 'Contains' takes 2 arguments Why do i get this error ?
8th Nov 2022, 7:55 AM
sneeze
sneeze - avatar
+ 1
Thanks. Visual studio does support .Net Core, but the project am working in uses .Net Framework. Indexof works fine. Thank you for the explanation.
8th Nov 2022, 12:13 PM
sneeze
sneeze - avatar