How to compare two string marking the differences?

3

Let's say I have strings :

Oi, meu nome é ítalo and Olá, meu nome é ítalo

How to generate a + result - like this:

Hi , my name is Italo

Hello , my name is Italo

The system should mark somehow the part of string which is different.

Before anyone asks what I've done, I mean I have no idea how to do it, so I posted the question.

    
asked by anonymous 27.09.2017 / 02:23

2 answers

2

I just started to do the code here, but I can not stop it for now, it follows the initial code:

    private void Comparar(RichTextBox rtb1, RichTextBox rtb2)
    {
        rtb1.ForeColor = Color.Black;
        rtb1.BackColor = Color.Empty;

        string[] texto1 = rtb1.Text.Split(' ');
        string[] texto2 = rtb2.Text.Split(' ');
        int removidas = 0;
        for (int i =0 ; i < texto1.Length;i++)
        {
            if (texto2.Length > i - removidas)
            if (texto1[i] != texto2[i-removidas])
            {
                rtb1.Select(rtb1.Text.IndexOf(texto1[i], i), texto1[i].Length);
                rtb1.SelectionBackColor = Color.Orange;
                removidas++;
            }
        }

    }

Obviously it's still just a prototype, but you can start. The result was as follows:

    
27.09.2017 / 03:36
4

You can use a library that manages textual comparison.

The google-diff-match-patch library is available in a number of languages .

To use the C # version, simply download the latest version of the diff_match_patch.zip file and add the DiffMatchPatch.cs file to your project.

The comparison can be made as follows:

string text1 = "Oi, tudo bem?";
string text2 = "Olá, tudo bem?";

var dmp = new diff_match_patch();
var diffs = dmp.diff_main(text1, text2);
var html = dmp.diff_prettyHtml(diffs);
    
27.09.2017 / 04:25