日记越累
2020-03-04
1. 字符串对比,是否相同
int string.Compare(string str1,string str2,ignoreCase:bool)
运用实例:
/// <summary> /// 是否超级管理员 /// </summary> public bool IsSuperAdmin => string.Compare(UserId, Role.DefaultId, ignoreCase: true) == 0 || string.Compare(UserRole?.Id, Role.DefaultId, ignoreCase: true) == 0;
第三个参数如果为是否忽略大小写....
返回值为int,为0则完全相同,大于0或小于0都不相同,所以只要不等于0都是false
简单例子:
namespace ConsoleApp1 { class Program { static void Main(string[] args) { string str1 = "zhang"; string str2 = "Zhang"; int i = string.Compare(str1, str2, ignoreCase: true); bool res = i == 0 ? true : false; Console.WriteLine(res); Console.ReadLine(); } } }
2.