string1="a=2,b=3,c=4,"
string2="a=4,b=2,c=9,"
比较结果是"a=[不同1],b=[不同2],c=[不同3]," ?
9 个解决方案
#1
C#本身就有这个功能
#2
没有直接的方法吧,应该先对
string1,string2用split拆分,再比较
string1,string2用split拆分,再比较
#3
怎么调用 ?
#4
string string1 = "a=2,b=3,c=4,";
string str = string1;
int i = 0;
var list = Regex.Matches("a=4,b=2,c=9,", @"(?i)(?<=,|^)([a-z]+=)(\d+)(,|$)").OfType<Match>().Select(t => new { txt = t.Value, left = t.Groups[1].Value }).ToList();
str = Regex.Replace(str, @"(?i)(?<=,|^)([a-z]+=)(\d+)(,|$)", delegate(Match m)
{
string left = m.Groups[1].Value;
if (list.Count(t => t.left == left) < 1)
return left + "[不包含]" + m.Groups[3].Value;
return m.Groups[1].Value + (list.Count(t => t.txt == m.Value) > 0 ? "[相同]" : string.Format("[不同{0}]", ++i)) + m.Groups[3].Value;
});
Console.WriteLine(str);
#5
不行。
换了组字符串,就得不到正确的结果了
#6
可以用linq,写个自定义比较器
#7
使用分词组件吧,一般的比较没有效果。
#8
linq可以
#9
static void Main(string[] args)
{
string temA = "a=1,b=2,c=3,d=4,e=5";
string temB = "a=1,b=4,c=3,d=4,e=10";
string[] a = temA.Split('=', ',');
string[] b = temB.Split('=', ',');
string line = "";
for (int i = 1, j = 0, k = 0; i < a.Length; i++)
{
if (a[i] == b[i])
{
j++;
line = line + a[i - 1] + "=" + "相等" + j + " ";
}
else
{
k++;
line = line + a[i - 1] + "=" + "不想等" + k + " ";
}
i++;
}
Console.WriteLine(line);
Console.ReadLine();
}
#1
C#本身就有这个功能
#2
没有直接的方法吧,应该先对
string1,string2用split拆分,再比较
string1,string2用split拆分,再比较
#3
怎么调用 ?
#4
string string1 = "a=2,b=3,c=4,";
string str = string1;
int i = 0;
var list = Regex.Matches("a=4,b=2,c=9,", @"(?i)(?<=,|^)([a-z]+=)(\d+)(,|$)").OfType<Match>().Select(t => new { txt = t.Value, left = t.Groups[1].Value }).ToList();
str = Regex.Replace(str, @"(?i)(?<=,|^)([a-z]+=)(\d+)(,|$)", delegate(Match m)
{
string left = m.Groups[1].Value;
if (list.Count(t => t.left == left) < 1)
return left + "[不包含]" + m.Groups[3].Value;
return m.Groups[1].Value + (list.Count(t => t.txt == m.Value) > 0 ? "[相同]" : string.Format("[不同{0}]", ++i)) + m.Groups[3].Value;
});
Console.WriteLine(str);
#5
不行。
换了组字符串,就得不到正确的结果了
#6
可以用linq,写个自定义比较器
#7
使用分词组件吧,一般的比较没有效果。
#8
linq可以
#9
static void Main(string[] args)
{
string temA = "a=1,b=2,c=3,d=4,e=5";
string temB = "a=1,b=4,c=3,d=4,e=10";
string[] a = temA.Split('=', ',');
string[] b = temB.Split('=', ',');
string line = "";
for (int i = 1, j = 0, k = 0; i < a.Length; i++)
{
if (a[i] == b[i])
{
j++;
line = line + a[i - 1] + "=" + "相等" + j + " ";
}
else
{
k++;
line = line + a[i - 1] + "=" + "不想等" + k + " ";
}
i++;
}
Console.WriteLine(line);
Console.ReadLine();
}