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
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.
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();
}
}
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;