Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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"
Posted
Comments
Zoltán Zörgő 4-Nov-13 2:09am    
First of all, define the "difference" properly. The sample you had given is simple - are all possible inputs that simple?
Do you want to treat the input as set of characters or are there any semantic or syntax involved?
Character case matters?
Is the function symmetric f(A,B)=f(B,A)?
Does character order matter in the input or result?
And so on...
Will this definition stand: get all characters from the one string that is not in the other? Like: f("ABCD","DEA")="BCE".

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.
 
Share this answer
 
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:
C#
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....
 
Share this answer
 
C#
string a = "ABCDE";
string b = "ABCE";

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

Console.Read();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900