0

Sample Output image

In my requirement i have two strings (Dynamically) i want to compare two strings and strike off the deleted/modified string and also highlight the newly added string. one string is my old string and one string is new string, some times both are same based on user input. i tried but i cant get output. please help me. below is my tried code in c#

Ex: string s1 = "Hello dear Alice and dear Bob.";string s2 = " Hello Alice and dear Bob Have a nice day.";

Need Output: Hello dear Alice and dear Bob Have a nice day. Dear is strike off and Have a nice day is highlight. pLEASE help me friends My code:

if(String.Equals(my_NString,my_String,StringComparison.OrdinalIgnoreCase))
    {
    sb.AppendLine("<div><p style='text-align:justify;'>"+my_NString+" </p></div>");
    sb.AppendLine("<br/>");
    }
    else
    {
    sb.AppendLine("<p style='text-align:justify;border:3px;border-color:#FF0000;padding:1em;color:red;'>"+my_NString+" </p>");
    sb.AppendLine("<br/>");
    }
}
2
  • Could you show your output ? Commented Jul 12, 2018 at 12:17
  • Please Refresh browser and click on sample Output image,just now i uploaded image Commented Jul 12, 2018 at 12:21

1 Answer 1

1

This is not so easy. There's no one way to compare strings like that. There are different strategies and each have their up and downs. This is a very complicated task. Your best shot is to use an existing implementation of difference and variation algorithm, like this: https://github.com/kpdecker/jsdiff (sorry, it's js)

PS: Editted:

Example really depends on what library/engine you'd like to use. The one that I'm most familiar with (and used most often) would look like this:

class Difference {
  public static void Main(string[] args) {
    diff_match_patch match = new diff_match_patch();
    List<Diff> diff = match.diff_main("Hello World.", "Goodbye World.");

    for (int i = 0; i < diff.Count; i++) Console.WriteLine(diff[i]);
  }
}

The result would be:

[(-1, "Hell"), (1, "G"), (0, "o"), (1, "odbye"), (0, " World.")]

You could also use match.diff_cleanupSemantic(diff); before displaying, and then the result would be:

[(-1, "Hello"), (1, "Goodbye"), (0, " World.")]

So basically use diff_cleanupSemantic to change level of differences from 'letter-level' to 'word-level'.

Sign up to request clarification or add additional context in comments.

11 Comments

any other possibilites
i got the deleted words in to list , i need if both deleted,added string or idle string below is my code
List<string> diff; IEnumerable<string> set1 = my_NString.Split(' ').Distinct(); IEnumerable<string> set2 = my_String.Split(' ').Distinct(); if (set2.Count() > set1.Count()) { diff = set2.Except(set1).ToList(); } else { diff = set1.Except(set2).ToList(); }
@chaitanay Your soltion is not so good, because if you had 100 words, and only 1 first would differ, then 99 of them would be crossed out and then placed again. So not so good. I pasted a link to jsdiff, use it :)
Ok thanks. but jsdiff is for javascript side i need c#
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.