string UserName="孟岩王金龙赵海伟王楠"
string UserName2="王金龙孟岩赵海伟";
我想要比较出 UserName3="王楠";
或者UserName="孟岩,王金龙,赵海伟,王楠"
UserName2="王金龙,孟岩,赵海伟";也可以
因为王金龙也有个王字 我现在取得结果最后只有一个楠 求大神解决
string UserName = "孟岩,王金龙,赵海伟,王楠";
string UserName2 = "王金龙,孟岩,赵海伟";
string[] usernames = UserName.Split(',');
string[] usernames2 = UserName2.Split(',');
for (int i = 0; i < usernames.Length; i++)
{
string name = usernames[i];
if (!usernames2.Contains(name))
{
Console.WriteLine(name);
}
}
string UserName = "孟岩,王金龙,赵海伟,王楠"; string UserName2 = "王金龙,孟岩,赵海伟"; string[] result1 = UserName.Split(','); string[] result2 = UserName2.Split(','); string notIn1="", notIn2 = ""; foreach (var name1 in result1) { bool found = false; foreach (var name2 in result2) { if(name1== name2) { found = true; } } if(!found) { notIn1 += name1; } } foreach (var name2 in result2) { bool found = false; foreach (var name1 in result1) { if (name1 == name2) { found = true; } } if (!found) { notIn2 += name2; } } //notIn1,notIn2 就是不同的
notIn1,notIn2 就是不同的
var UserName = "孟岩,王金龙,赵海伟,王楠"; var UserName2 = "王金龙,孟岩,赵海伟"; var a = UserName.Split(',').ToList(); var b = UserName2.Split(',').ToList(); var result = new List<string>(); for (int i = 0; i < b.Count; i++) { for (int j = 0; j < a.Count; j++) { if (b[i] == a[j]) { a.Remove(a[j]); break; } } } result = a;