❔ How can I diff 2 strings?

How can I find the difference between two strings (added/removed/modified characters/words)
14 Replies
phaseshift
phaseshift13mo ago
use git
Stan
Stan13mo ago
lel
kenan - Shortcut
kenan - ShortcutOP13mo ago
lol, but seriously, I'd like to do it in C#
Stan
Stan13mo ago
depends how accurate you want it, its gonna get a bit complex
phaseshift
phaseshift13mo ago
it's non-trivial if youre diffing e.g. multiple words, paragraphs
kenan - Shortcut
kenan - ShortcutOP13mo ago
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
phaseshift
phaseshift13mo ago
yeah, libgit
Stan
Stan13mo ago
honestly yeah, if theres a library for it, save yourself the trouble xD unless its like a code exercise kinda thing
kenan - Shortcut
kenan - ShortcutOP13mo ago
I've also found DiffPlex, is it any good?
Stan
Stan13mo ago
rule of thumb, go by most stars or downloads
kenan - Shortcut
kenan - ShortcutOP13mo ago
It has a decent amount of stars
No description
Stan
Stan13mo ago
read the docs, does it look usable enough for you?
Bailey
Bailey13mo ago
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( );
Accord
Accord13mo ago
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.
Want results from more Discord servers?
Add your server