1

I need some library support to calculate difference between string1 & string2. And use the difference result later to calculate string2 using string1 & diff result.

Actually, I need to implement something like post edits history like here at StackOverflow. I want to keep in DB the recent copy of post & difference w.r.t to old post. And use both of them whenever I need to see the old copy of post.

Have you heard of any libraries that can help me out to calculate string difference ?

Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294
  • 1
    This question seems to be helpful: http://stackoverflow.com/questions/132478/how-to-perform-string-diffs-in-java – christopher May 10 '13 at 13:47
  • 1
    This question has been answered before in [this thread](http://stackoverflow.com/questions/132478/how-to-perform-string-diffs-in-java) – Majid Abarghooei May 10 '13 at 13:47

1 Answers1

0

You could use Levenshtein distances to calculate differences between strings.

Theory.

Example in Javascript (which you can adapt to Java fairly easily - there are examples in other languages in the wiki above too).

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
  • I'm not sure if a javascript implementation is relevant in a Java question. – christopher May 10 '13 at 13:43
  • 2
    @ChrisCooney Similar languages (in syntax), and you'd take just a few seconds to port the code from one into the other. – Geeky Guy May 10 '13 at 13:45
  • -1 but how do you reconstruct the other string from the string and the diff? levenstein is a one-way calculation – Bohemian May 10 '13 at 14:25
  • Then change the algorithm to return the difference matrix, not just the result. You can infer the indexes of the changes from there. – Geeky Guy May 10 '13 at 17:19
  • Then it's no longer Levenstein. It's something else. At a fundamental level, Levenstein is useless, because it's a lossy compression of the difference - like a hash. It is strictly one-way. Reconstituting one string from the other is not possible using any incantation of Levenstein. – Bohemian May 11 '13 at 11:37