Avatar ·

How to make Contains(string) method case-insensitive in C#?

Hi everyone! I'm a beginner C# developer and I've encountered a problem that seems simple, but I just can't find the right solution.\n\nI have a list of strings, and I want to check if each string contains a specific word. The problem is that the case can be different. For example, I have the string "Hello World", and I'm searching for "world" (lowercase). By default, the Contains method returns false because it is case-sensitive.\n\nHere's an example of my code:\n\n```csharp\nstring text = "Привет, Мир!";\nstring searchTerm = "мир";\n\nif (text.Contains(searchTerm))\n{\n Console.WriteLine("Found!");\n}\nelse\n{\n Console.WriteLine("Not found.");\n}\n```\n\nIn this case, "мир" will not be found, even though the string contains "Мир" with a capital letter. I tried using ToLower() for both