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

后端 未结 8 981
陌清茗
陌清茗 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'");
            }
        }
    }
    

提交回复
热议问题