Compare two strings and get the difference

How can i compare two strings in c# and gets the difference?

for example:

String1 : i have a car

string2 : i have a new car bmw

result: new, bmw


Solution 1:

You need to make sure the larger set is on the left hand side of the Except (not sure if there is a pure Linq way to achieve that):

    static void Main(string[] args)
    {
        string s1 = "i have a car a car";
        string s2 = "i have a new car bmw";

        List<string> diff;
        IEnumerable<string> set1 = s1.Split(' ').Distinct();
        IEnumerable<string> set2 = s2.Split(' ').Distinct();

        if (set2.Count() > set1.Count())
        {
            diff = set2.Except(set1).ToList();
        }
        else
        {
            diff = set1.Except(set2).ToList();
        }
    }

Solution 2:

Based off your question (It is a bit vague.) this should work.

var first = string1.Split(' ');
var second = string2.Split(' ');
var primary = first.Length > second.Length ? first : second;
var secondary = primary == second ? first : second;
var difference = primary.Except(secondary).ToArray();

At the top of your file make sure you include:

using System.Linq;

Solution 3:

You can use a difference algorithm for this task. The paper "An O(ND) Difference Algorithm and Its Variations" describes a quite powerful algorithm to accomplish this task. For an implementation in C#, you can have a look at "An O(ND) Difference Algorithm for C#", but IMHO it surely is more interesting to have a look at the paper and implement it for yourself in case you're interested on how the algorithm works in detail.

Solution 4:

I had some null point exception with null strings so I improved Mitch Wheat answer to handle this problem too:

public static string Difference(string str1, string str2)
{
    if (str1 == null)
    {
        return str2;
    }
    if (str2 == null)
    {
        return str1;
    }
    
    List<string> set1 = str1.Split(' ').Distinct().ToList();
    List<string> set2 = str2.Split(' ').Distinct().ToList();

    var diff = set2.Count() > set1.Count() ? set2.Except(set1).ToList() : set1.Except(set2).ToList();

    return string.Join("", diff);
}