I am using a Diffplex tool to compare two strings to see which words have been added, deleted etc. I can get the words or sentences which have been added or deleted with ease and display them in an html pre tag. What I was using is appending a + and - using string builder on words added and removed respectively and indeed its showing without a problem. The code is below;
StringBuilder sb = new StringBuilder();
string oldText = OldContent;
string newText = newContent;
var d = new Differ();
var builder = new InlineDiffBuilder(d);
var result = builder.BuildDiffModel(oldText, newText);
foreach (var line in result.Lines)
{
if (line.Type == ChangeType.Inserted)
{
sb.Append("+ ");
}
else if (line.Type == ChangeType.Deleted)
{
sb.Append("- ");
}
else if (line.Type == ChangeType.Modified)
{
sb.Append("* ");
}
else if (line.Type == ChangeType.Imaginary)
{
sb.Append("? ");
}
else if (line.Type == ChangeType.Unchanged)
{
//sb.Append(" ");
}
sb.Append(line.Text + "\n");
ViewBag.result = sb.ToString();
I am returning the string to an html pre tag like below;
<pre>@ViewBag.result</pre>
Below is a sample output of the result
+ This line has been added
- This line has been deleted
The + and - are showing at the start of the sentences without a problem. Now My question; Is it possible to output a sentence or words which I am appending the + symbol green and the one with - symbol red? If so how can I approach this and can I append the styling using the string builder then send it and If so how can I approach this?