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

0

2 Answers 2

8

Without the need for any conversion:

bool found = "My Name is".IndexOf("name", StringComparison.OrdinalIgnoreCase) >= 0;
Sign up to request clarification or add additional context in comments.

Comments

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'");
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.