Link to home
Create AccountLog in
C#

C#

--

Questions

--

Followers

Top Experts

Avatar of tjrobinson
tjrobinson

Comparing two strings and highlighting differences
I'm writing a very basic content management system and I want to be able to show two different versions of  some text and highlight additions, changes and deletions. Are there any controls or code samples that could help me with this? My initial Google search was fruitless.

I'm storing the content with HTML markup, although the user shouldn't see this as they won't understand it.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of Bob LearnedBob Learned🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of tjrobinsontjrobinson

ASKER

Thanks Bob.

I've had a quick play and I've just about got it to compare two simple strings. The only problem with it for my situation is that I can't find a way of using it to deal with continuous strings like the ones I'm using. My content is stored as HTML, edited by FreeTextBox. That means that everything is effectively on one line, so  MeneesDiff can tell me if there is a difference, but not the character positions of those differences.

Here's an example of my content:

Before:
<p>Hello, I'm a message.</p>

After:
<p>Hello, I'm a message with an extra bit.</p>

So I'm looking for something like this as the output:

<p>Hello, I'm a message<span class="color: green"> with an extra bit.</span></p>


Avatar of Bob LearnedBob Learned🇺🇸

Question about this case:

Before:
<p>Hello, I'm a message.  This is a test</p>

After:
<p>Hello, I'm a message with an extra bit.  This is a test.</p>

How would you like to handle that case?  Would you like to perform this function on a sentence-by-sentence case, word-by-word case, or a character-by-character case?

Bob




I think it'd be character-by-character, ignoring the HTML markup.

If new characters have been added then they would be coloured green by a HTML span tag.

If characters have been taken away they would be styled by a strikethrough using a HTML span tag.

If characters have been changed then ideally they'd be yellow, but that might be more difficult to implement.

Whitespace would be ignored, case wouldn't.

I'm starting to think this is going to be a lot harder than I first imagined.


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Bob LearnedBob Learned🇺🇸

Case-sensitive or case-insensitive?

Examples:

'-' is deleted char (strike-through)
'»' is new char (green)
'•' is changed char (yellow)


Before:                             After:                             Highlight:
===============     ==============      ======
This is a test                     Test                               ----------•est
Spread                             Read                              -•ead
Tree                                Trees                             Tree»
Frees                               Freed                             Free•

Are these valid assumptions?

Bob

Those are valid assumptions, although it would be case-sensitive.

The reason for making it case sensitive is so that when somebody comes to review the content, they can see what has changed at a quick glance, i.e. missing capital letter on a place name.
C#

C#

--

Questions

--

Followers

Top Experts

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).