C#比较字符串 官网
在C#里,比较字符串是常见操作。可使用Equals方法来判断两个字符串是否相等。 string str1 = "hello";string str2 = "hello";bool result = str1.Equals(str2); // 运行结果: trueConsole.WriteLine(result); 也能使用Compare方法,它会返回一个整数,小于0表示str1小于str2,等于0表示相等,大于0表示str1大于str2。 string str3 = "apple";string str4 = "banana";int compareResult = string.Compare(str3, str4); // 运行结果: 负数Console.WriteLine(compareResult); 比较时要注意大小写,默认是区分大小写的。若要不区分大小写,可使用StringComparison.OrdinalIgnoreCase参数。