C
C#2y ago
kenan239

❔ How can I diff 2 strings?

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

Did you find this page helpful?