String comparisons

Status
Not open for further replies.

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
Repeatedly, I keep running into versions of the same basic problem while coding:
I have two strings that are similar, but not identical, and I need to be able to highlight the similarities and/or the differences.

Two basic examples:
  • I'm debugging a WPF Rich TextBox bug where the framework is occasionally inserting a random additional space into the content. When I detect that this has happened, I need to quickly highlight where the additional space is at.
  • I'm importing user created files from users users who only loosely follow naming conventions. If I'm scanning a list of files for the one with Dr. Troshane's Address, I'd like to be able to auto-pick any candidate files with names like: Troshane_Address.xml, Troshane Address 1.xml or AddrTroshane.xml. (IOW, look to see if there is a file with a high probability of being the right one, and auto-pick it so the operator's workload of manually selecting which import file goes to what will be greatly reduced.)

1) Are there any standard algorithms for this problem domain? I've reinvented this wheel dozens of times over the years and am tired of re-thinking it every few months when it reoccurs in another program. I'd love to just implement something standard (or find an implementation of something) and just re-use it.

2) Are there any Visual Studio add-ins that do this type of thing in the debugger? I'd love a magic "visually compare two strings" tool while debugging.
 

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
Richard Berg":1j8glcde said:
I assume this is for visualization, not an intermediate step in some algorithm? If so, just steal code from KDiff.
Sometimes yes, sometimes no. In the debugger, I just want a visualization. In other places, it is an intermediate stage of an algorithm (E.G. The above example of auto-picking files.)
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
I had to do this in a quick and dirty way and lifted some code from google code in java that would return you a list of edit locations (and the length of the edit) and converted that into a simple string of spaces and '^' to indicate the diffs.

I'm afraid I can't remember which it was but IIRC it was in java and I did a very quick and dirty transcription to c#.

It wasn't complex, I did the whole thing in one LINQPad file.
 
Status
Not open for further replies.