我使用diff-match-patch (https://code.google.com/archive/p/google-diff-match-patch/)来获取两个文本之间的差异。在diff的末尾,它们返回奇怪的字符:例如à成为%C3%A0,ù %C3%B9," %22等等。
这是我的代码示例:
String startDocument = "hello world";
String endDocument = "àèìòù\"";
diff_match_patch dmp = new diff_match_patch();
dmp.Diff_Timeout = 16;
LinkedList<Diff> diffs = dmp.diff_main( startDocument, endDocument );
String diff = dmp.diff_toDelta(diffs);
System.out.println(diff); //return: -11 +%C3%A0%C3%A8%C3%AC%C3%B2%C3%B9%22如何检索原始字符?
发布于 2010-06-23 22:49:52
试一试
javac -encoding utf8 DaClass.java和
java -Dfile.encoding=utf8 DaClass发布于 2014-05-29 23:27:51
这是预期的行为。
DiffMatchPatch使用类似javascript的特殊字符编码(来自the project's wiki):
特殊字符使用%xx表示法进行编码。除了不是encoded.的空格之外,编码的字符集与JavaScript的encodeURI()函数匹配
要对其进行解码,只需查看the code
// decode would change all "+" to " "
param = param.replace("+", "%2B");
try {
param = URLDecoder.decode(param, "UTF-8");
} catch (UnsupportedEncodingException e) {
// Not likely on modern system.
throw new Error("This system does not support UTF-8.", e);
} catch (IllegalArgumentException e) {
// Malformed URI sequence.
throw new IllegalArgumentException("Illegal escape in diff_fromDelta: " + param, e);
}
diffs.add(new Diff(Operation.INSERT, param));实际上,您不需要解码等式或移除,因为它不包括增量格式的文本。
如果您只是想显示不同之处,请查看diff_prettyHtml方法。
https://stackoverflow.com/questions/3102669
复制相似问题