If I have two strings that are both comma separated values and I know there will only be 1 part missing from one of the strings, how can I retrieve that in C#?
Example:
String 1 = "one,two,four"
String 2 = "one,two,three,four"
Result = "three"
I've tried using String.Compare
which returns -1 or 1 so I know they are different, but how do I actually pull the value of the difference?
I'd like to have a 3rd string containing the missing value from String 1
Thanks in advance.
string result = null;
string[] arr1 = 1.Split(','); //Get the first string into array
string[] arr2 = 2.Split(','); //Get the second string into array
//Compare lengths, if arr1 is bigger call Except on it, else on arr2
//Linq .Except will return elements of array which aren't in the parameter array
if(arr1.Length > arr2.Length
{
result = String.Join(" ", arr1.Except(arr2).ToArray());
}
else
{
result = String.Join(" ", arr2.Except(arr1).ToArray());
}