How to case-insensitively check if a character exists in a string with C#

1 Answer

0 votes
using System;

class Program
{
    // Case-insensitive check without a loop
    static bool CharExistsIgnoreCase(string s, char target) {
        // Convert both to lowercase and use Contains()
        return s.ToLower().Contains(char.ToLower(target));
    }

    static void Main()
    {
        // Define the string we want to search in
        string s = "CSharpLanguage";

        // Perform the case-insensitive check
        bool exists = CharExistsIgnoreCase(s, 'c');

        // Print the raw boolean result
        Console.WriteLine(exists);

        // Conditional check
        if (exists) {
            Console.WriteLine("exists");
        }
        else {
            Console.WriteLine("not exists");
        }
    }
}



/*
run:

True
exists

*/

 



answered 3 hours ago by avibootz

Related questions

...