C#判断两个字符串是否相等的方法


                 //第一种判断方式
                int result1 = str1.CompareTo(str2);
                Console.WriteLine(result1);   //输出result1=0

                //第二种判断方式
                int result2 = String.Compare(str1, str2);
                Console.WriteLine(result2);   //输出result2=0    

                //后面两种方式需要注意str1和str2是不是为null的情况

                //第三种判断方式
                bool result3 = str1.Equals(str2);
                Console.WriteLine(result3);   //输出result3=true

                //第四种判断方式
                bool result4 = String.Equals(str1, str2);
                Console.WriteLine(result4);   //输出result4=true

               以上四种方法看着是不是要比 str1==str2,要专业一些呀

猜你喜欢

转载自blog.csdn.net/yanghezheng/article/details/109542553