google-diff-match-patch使用说明

要对文本文件的进行比较的时候,可以考虑使用google-diff-match-patch算法,它可以对文本文件进行比较、匹配和生成补丁的操作。

他的官网是:http://code.google.com/p/google-diff-match-patch/

CSDN下载:http://download.csdn.net/detail/modern1314/5323399


使用google diff的话,这个差异就是从左边的字符串变成右边的字符串所需要的最少的步骤,每个步骤只能做“保持不变”、“插入”或者“删除”操作。



这里举个简单的例子:

我们把一个字符串文本Text 和基本字符串对比生成补丁后,然后拿着补丁和基本字符串恢复 字符串Text。

public void testDiff(){	  
	  String commonStr = "I am jack";
	  String text1 ="I am  a coder";
	  LinkedList<Diff> diffs = dmp.diff_main(commonStr, text1);
	  
	  for(Diff diff : diffs){
		  System.out.println(diff.toString());
	  }
	  LinkedList<Patch> patches = dmp.patch_make(diffs);
	  
	  String patchesStr = dmp.patch_toText(patches);
	  
	  patches = (LinkedList<Patch>)dmp.patch_fromText(patchesStr);
	  
	  Object[] results = dmp.patch_apply(patches, commonStr);
	  
	  System.out.println("result = " + results[0]);
	  
  }

单测结果如下:



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值