❔ How can I diff 2 strings?
How can I find the difference between two strings (added/removed/modified characters/words)
14 Replies
use git
lel
lol, but seriously, I'd like to do it in C#
depends how accurate you want it, its gonna get a bit complex
it's non-trivial if youre diffing e.g. multiple words, paragraphs
Doesn't have to be accurate, just good enough to be able to recognize modified characters from one another.
I tried writing my own algorithm for this and it turned out horrible so I'm asking if there are better solutions
yeah, libgit
honestly yeah, if theres a library for it, save yourself the trouble xD
unless its like a code exercise kinda thing
I've also found DiffPlex, is it any good?
rule of thumb, go by most stars or downloads
It has a decent amount of stars
read the docs, does it look usable enough for you?
The best advice is use a library, However if it is to learn, the following is a start.
However keep in mind, if there is a word added, then it wont work and with libraries there ie an algorithm which solve this.
var unmoddifiedSentance = "th1s 1s just an example how to start";
var moddifiedSentance = "this is just a example how to start";
List<string> unmoddified = unmoddifiedSentance.Split(' ').ToList();
List<string> moddified = moddifiedSentance.Split(' ').ToList();
for (int i = 0; i < unmoddified.Count; i++)
if (unmoddified[i] != moddified[i])
Console.WriteLine($"moddified word was: {unmoddified[i]} is {moddified[i]} ");
// when you know the word is different you van search for the char
Console.ReadLine( );
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.