Compare two strings and get the difference

前端 未结 3 1899
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 17:55

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

相关标签:
3条回答
  • 2020-12-01 18:14

    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.

    0 讨论(0)
  • 2020-12-01 18:29

    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();
            }
        }
    
    0 讨论(0)
  • 2020-12-01 18:35

    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;
    
    0 讨论(0)
提交回复
热议问题