Thursday, May 7, 2015

How do i compare two strings and get the difference?

If I guess that what you want is to check that s2 is a substring of s1, and if so remove the substring from s1, here is some code to do that.

int pos = s1.IndexOf(s2);
if (pos < 0)
{
    throw new InvalidOperationException(s2 + " is not a substring of " + s1);
}
string strDiff = s1.Remove(pos, s2.Length);

No comments:

Post a Comment