2

I want to check whether string below contains top/ TOP/toP/ Top/TOp/ Top in c#. My code is like

string str = null;
        str = "CSharp Top11111 10 BOOKS";
        if (str.Contains("top") == true)
        {
            Console.WriteLine("The string Contains() 'TOP' ");
        }
        else
        {
            Console.WriteLine("The String does not Contains() 'TOP'");
        }

But it return true only when my string contain 'top'. How can return true for all other scenarios too? I know this may be simple, but I searched a Lot didn't find any solutions

S M
  • 3,133
  • 5
  • 30
  • 59

2 Answers2

8

Without the need for any conversion:

bool found = "My Name is".IndexOf("name", StringComparison.OrdinalIgnoreCase) >= 0;
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
4

Use one of both: .ToLower() or .ToUpper

string str = null;
    str = "CSharp Top11111 10 BOOKS";
    if (str.ToLower().Contains("top") == true)
    {
        Console.WriteLine("The string Contains() 'TOP' ");
    }
    else
    {
        Console.WriteLine("The String does not Contains() 'TOP'");
    }
Marcel B
  • 508
  • 2
  • 9