How can I check if a string contains a character in C#?

后端 未结 8 979
陌清茗
陌清茗 2020-12-02 09:43

Is there a function I can apply to a string that will return true of false if a string contains a character.

I have strings with one or more character options such

相关标签:
8条回答
  • 2020-12-02 10:30

    here is an example what most of have done

    using System;
    
    class Program
    {
        static void Main()
        {
            Test("Dot Net Perls");
            Test("dot net perls");
        }
    
        static void Test(string input)
        {
            Console.Write("--- ");
            Console.Write(input);
            Console.WriteLine(" ---");
            //
            // See if the string contains 'Net'
            //
            bool contains = input.Contains("Net");
            //
            // Write the result
            //
            Console.Write("Contains 'Net': ");
            Console.WriteLine(contains);
            //
            // See if the string contains 'perls' lowercase
            //
            if (input.Contains("perls"))
            {
                Console.WriteLine("Contains 'perls'");
            }
            //
            // See if the string contains 'Dot'
            //
            if (!input.Contains("Dot"))
            {
                Console.WriteLine("Doesn't Contain 'Dot'");
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 10:31

    The following should work:

    var abc = "sAb";
    bool exists = abc.IndexOf("ab", StringComparison.CurrentCultureIgnoreCase) > -1;
    
    0 讨论(0)
提交回复
热议问题