如何比较字符串并获得不同的值? [英] How to Compare String and get differed value?

查看:103
本文介绍了如何比较字符串并获得不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#编码





string a =ABCE;

string b = ABCDE





如何比较上面的字符串并得到不同的值?

和上面的输出是D

解决方案

这并不像你想象的那么简单:有两个差异 ?究竟是什么定义了差异?

这里有通用算法: http:// en.wikipedia.org/wiki/Diff#Algorithm [ ^ ]

你会在这里找到一个实现: C#的O(ND)差分算法 [ ^ ]



对于你想要的东西可能有点重要,但它确实有这个工作。


ZoltánZörgő在上面问了一些非常好的问题。

我将在这里假设一些答案...

如果将两个字符串视为两组字符,并且您只想知道两个字符串中不存在哪些字符,那么:

  string  StringSetDifference( string  a, string  b)
{
var aEnumerable = a as IEnumerable< char> ;;
var bEnumerable = b as IEnumerable< char> ;;
var allChars = new HashSet< char>(aEnumerable);
allChars.UnionWith(bEnumerable); // 所有字符都存在
var bothChars = new HashSet< char>(aEnumerable);
bothChars.IntersectWith(bEnumerable); // 两者中出现的字符
allChars.ExceptWith(bothChars); // 从一组中删除常用字符,只留下差异
return string .Concat(allChars as IEnumerable< char> );



如果你想跟踪差异的位置,例如:

ABCDEFDG之间的区别和ABCDEF将是DG。

上面的算法不能正确,因为D出现两次而 HashSet 不关心重复。

这是一个重要的计算机科学研究领域。

见书:字符串,树和序列的算法:计算机科学和Dan Gusfield的计算生物学(1997) [ ^ ]以及Original Griff提供的参考资料....


  string  a =   ABCDE; 
string b = ABCE;

foreach var item in a。除了(b))
{
Console.WriteLine(item);
}

Console.Read();


By using C# coding


string a= "ABCE";
string b= "ABCDE"


how to compare above string and get the differed value??
and output for the above is "D"

解决方案

This is not as simple as you think: what is there are two "differences"? What defines a "difference" anyway?
There are general purpose algorithms for this: http://en.wikipedia.org/wiki/Diff#Algorithm[^]
and you will find an implementation here: An O(ND) Difference Algorithm for C#[^]

It may be a bit heavy duty for what you want, but it does do the job.


Zoltán Zörgő asked some very good questions above.
I'm going to assume some answers here...
If you treat the two strings as two sets of characters and you just want to know which characters are not present in both strings then:

string StringSetDifference(string a, string b)
{
  var aEnumerable = a as IEnumerable<char>;
  var bEnumerable = b as IEnumerable<char>;
  var allChars = new HashSet<char>(aEnumerable);
  allChars.UnionWith(bEnumerable);        // all chars present at all
  var bothChars = new HashSet<char>(aEnumerable);
  bothChars.IntersectWith(bEnumerable);   // the chars present in both
  allChars.ExceptWith(bothChars);         // remove the common chars from the set of all, leaving only the differences
  return string.Concat(allChars as IEnumerable<char>);


If you want to keep track of the positions of the differences as well, for example:
The difference between "ABCDEFDG" and "ABCDEF" would be "DG".
The algorithm above would not get this correct because the "D" occurs twice and the HashSet doesn't care about duplicates.
This is an area of significant research in computer science.
See the book: Algorithms on Strings, Trees and Sequences: Computer Science and Computational Biology by Dan Gusfield (1997)[^] as well as the references provided by Original Griff....


string a = "ABCDE";
string b = "ABCE";

foreach (var item in a.Except(b))
{
    Console.WriteLine(item);
}

Console.Read();


这篇关于如何比较字符串并获得不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆