字串比較的常用函式

CSF践行發表於2024-08-16
       static void Main(string[] arg)
        {
            int int1 = 0;
            int int2 = 0;
            int int3 = 0;
            string str1 = "adf";
            string str2 = "adf";
            string str3 = "Adf";
            string str4 = "Bdf";
            string str5 = "";
            //比較字串的常用函式:
            //(1) ==
            //(2) Int Compare(string strA,string strB);
            //(3) Int Compare(string strA,string strB,bool IgorCase)//忽略大小寫
            //(4) public Int CompareTo(string strB );
            //(5) public bool Equals(string value)
            //    public static bool Equals(string a, string b)


            //練習1
            Console.WriteLine(str1 == str2);

            //練習2
            Console.WriteLine(string.Compare("aaa", "aaa"));
            Console.WriteLine(string.Compare("aaa", "bbb"));
            Console.WriteLine(string.Compare("bbb", "aaa"));

            //練習3
            Console.WriteLine(string.Compare("aaa", "aAa", true));
            Console.WriteLine(string.Compare("aaa", "aAa", false));

            //練習4
            Console.WriteLine(str1.CompareTo(str2));

            //練習5
            Console.WriteLine(str1.Equals(str2));
            Console.WriteLine(string.Equals(str1, str2));

            Console.ReadKey();
        }

相關文章