Compare two strings and get the difference

前端 未结 3 1904
佛祖请我去吃肉
佛祖请我去吃肉 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.

提交回复
热议问题