我需要找到两个字符串之间的差异。
const string1 = 'lebronjames';
const string2 = 'lebronnjames';
期望的输出是找到额外的n
并将其记录到控制台。
在JavaScript中有没有实现这个功能的方法?
我需要找到两个字符串之间的差异。
const string1 = 'lebronjames';
const string2 = 'lebronnjames';
期望的输出是找到额外的n
并将其记录到控制台。
在JavaScript中有没有实现这个功能的方法?
let a = "thelebronnjamist";
let b = "the lebron james";
let difference = patienceDiff( a.split(""), b.split("") );
...使用difference.lines
,将其设置为一个数组,其中包含比较的结果...
difference.lines: Array(19)
0: {line: "t", aIndex: 0, bIndex: 0}
1: {line: "h", aIndex: 1, bIndex: 1}
2: {line: "e", aIndex: 2, bIndex: 2}
3: {line: " ", aIndex: -1, bIndex: 3}
4: {line: "l", aIndex: 3, bIndex: 4}
5: {line: "e", aIndex: 4, bIndex: 5}
6: {line: "b", aIndex: 5, bIndex: 6}
7: {line: "r", aIndex: 6, bIndex: 7}
8: {line: "o", aIndex: 7, bIndex: 8}
9: {line: "n", aIndex: 8, bIndex: 9}
10: {line: "n", aIndex: 9, bIndex: -1}
11: {line: " ", aIndex: -1, bIndex: 10}
12: {line: "j", aIndex: 10, bIndex: 11}
13: {line: "a", aIndex: 11, bIndex: 12}
14: {line: "m", aIndex: 12, bIndex: 13}
15: {line: "i", aIndex: 13, bIndex: -1}
16: {line: "e", aIndex: -1, bIndex: 14}
17: {line: "s", aIndex: 14, bIndex: 15}
18: {line: "t", aIndex: 15, bIndex: -1}
aIndex === -1
还是 bIndex === -1
,都表示两个字符串之间存在差异。具体来说...
b
中的位置是3。a
中的位置是9。b
中的位置是10。a
中的位置是13。b
中的位置是14。a
中的位置是15。请注意,PatienceDiff算法适用于比较两个相似的文本块或字符串。它不会告诉您是否发生了基本编辑。例如,以下内容...
let a = "james lebron";
let b = "lebron james";
let difference = patienceDiff( a.split(""), b.split("") );
返回包含difference.lines
的内容...
difference.lines: Array(18)
0: {line: "j", aIndex: 0, bIndex: -1}
1: {line: "a", aIndex: 1, bIndex: -1}
2: {line: "m", aIndex: 2, bIndex: -1}
3: {line: "e", aIndex: 3, bIndex: -1}
4: {line: "s", aIndex: 4, bIndex: -1}
5: {line: " ", aIndex: 5, bIndex: -1}
6: {line: "l", aIndex: 6, bIndex: 0}
7: {line: "e", aIndex: 7, bIndex: 1}
8: {line: "b", aIndex: 8, bIndex: 2}
9: {line: "r", aIndex: 9, bIndex: 3}
10: {line: "o", aIndex: 10, bIndex: 4}
11: {line: "n", aIndex: 11, bIndex: 5}
12: {line: " ", aIndex: -1, bIndex: 6}
13: {line: "j", aIndex: -1, bIndex: 7}
14: {line: "a", aIndex: -1, bIndex: 8}
15: {line: "m", aIndex: -1, bIndex: 9}
16: {line: "e", aIndex: -1, bIndex: 10}
17: {line: "s", aIndex: -1, bIndex: 11}
let a = "james lebron";
let b = "lebron james";
let difference = patienceDiffPlus( a.split(""), b.split("") );
...返回包含difference.lines
的内容...
difference.lines: Array(18)
0: {line: "j", aIndex: 0, bIndex: -1, moved: true}
1: {line: "a", aIndex: 1, bIndex: -1, moved: true}
2: {line: "m", aIndex: 2, bIndex: -1, moved: true}
3: {line: "e", aIndex: 3, bIndex: -1, moved: true}
4: {line: "s", aIndex: 4, bIndex: -1, moved: true}
5: {line: " ", aIndex: 5, bIndex: -1, moved: true}
6: {line: "l", aIndex: 6, bIndex: 0}
7: {line: "e", aIndex: 7, bIndex: 1}
8: {line: "b", aIndex: 8, bIndex: 2}
9: {line: "r", aIndex: 9, bIndex: 3}
10: {line: "o", aIndex: 10, bIndex: 4}
11: {line: "n", aIndex: 11, bIndex: 5}
12: {line: " ", aIndex: 5, bIndex: 6, moved: true}
13: {line: "j", aIndex: 0, bIndex: 7, moved: true}
14: {line: "a", aIndex: 1, bIndex: 8, moved: true}
15: {line: "m", aIndex: 2, bIndex: 9, moved: true}
16: {line: "e", aIndex: 3, bIndex: 10, moved: true}
17: {line: "s", aIndex: 4, bIndex: 11, moved: true}
注意添加了moved
属性,该属性标识一行(或字符)是否可能被移动。同样地,patienceDiffPlus
只是匹配了删除的aLines[]和添加的bLines[],因此不能保证这些行实际上被移动了,但很有可能它们确实被移动了。
patienceDiff
算法来识别可能移动的行/字符,而不仅仅是识别删除和添加。我已经在我的原始答案中附加了一个编辑,其中包含新的patienceDiffPlus
算法,并更新了我的GitHub存储库。 - Trentium.split(' ')
来标记单词,而不是字母。我们发现在突出显示长段落文本之间的差异时,这非常有用。感谢这个解决方案! - Jan Werkhoven这将返回两个字符串之间的第一个不同之处。
例如对于 lebronjames
和 lebronnjames
,结果为 n
。
const string1 = 'lebronjames';
const string2 = 'lebronnjabes';
const findFirstDiff = (str1, str2) =>
str2[[...str1].findIndex((el, index) => el !== str2[index])];
// equivalent of
const findFirstDiff2 = function(str1, str2) {
return str2[[...str1].findIndex(function(el, index) {
return el !== str2[index]
})];
}
console.log(findFirstDiff2(string1, string2));
console.log(findFirstDiff(string1, string2));
findIndex
来查找两个单词之间的第一个差异,然后使用索引 string[index]
。使用 esc6
语法,代码看起来更简洁。 - Aziz.G function getDifference(a, b)
{
var i = 0;
var j = 0;
var result = "";
while (j < b.length)
{
if (a[i] != b[j] || i == a.length)
result += b[j];
else
i++;
j++;
}
return result;
}
console.log(getDifference("lebronjames", "lebronnjames"));
想要返回两个字符串之间的第一个不同之处,可以像这样进行调整:
const getDifference = (s, t) => {
s = [...s].sort();
t = [...t].sort();
return t.find((char, i) => char !== s[i]);
};
console.log(getDifference('lebronjames', 'lebronnjames'));
console.log(getDifference('abc', 'abcd'));
const getDifference = (s, t) => {
let sum = t.charCodeAt(t.length - 1);
for (let j = 0; j < s.length; j++) {
sum -= s.charCodeAt(j);
sum += t.charCodeAt(j);
}
return String.fromCharCode(sum);
};
console.log(getDifference('lebronjames', 'lebronnjames'));
console.log(getDifference('abc', 'abcd'));
var findTheDifference = function(s, t) {
let res = [...s].sort();
let res1 = [...t].sort();
let j = 0;
while (j < res1.length) {
if (res[j] != res1[j]) {
return res1[j];
}
j++;
}
};
console.log(findTheDifference("a", "aa"))
function findDifference(s, t) {
if(s === '') return t;
// this is useless and can be omitted.
for(let i = 0; i < t.length; i++) {
if(!s.split('').includes(t[i])) {
return t[i];
}
}
// this is useless and can be omitted.
// (if the additional letter exists)
// cache them, count values, different values of the same letter would give the answer.
const obj_S = {};
const obj_T = {};
for(let i = 0; i < s.length; i++) {
if(!obj_S[s[i]]) {
obj_S[s[i]] = 1;
}else {
obj_S[s[i]]++;
}
}
for(let i = 0; i < t.length; i++) {
if(!obj_T[t[i]]) {
obj_T[t[i]] = 1;
}else {
obj_T[t[i]]++;
}
}
for(const key in obj_T) {
if(obj_T[key] !== obj_S[key]) {
return key
}
}
}
// if more than 1 letter -> store the values and the return, logic stays the same.
console.log(findDifference('john', 'johny')) // --> y
console.log(findDifference('bbcc', 'bbbcc')) //--> b
/** Calculates the maximum spanning diff of two strings, or `null` if identical
* @param {string} a
* @param {string} b */
function diff(a, b) {
if ((a.length === b.length) && (a == b)) { return null; } // Identical
let index = 0, endA = a.length, endB = b.length;
const minLen = Math.min(endA, endB);
while ((a[index] === b[index]) && (++index < minLen)); // Scan start
while ((endA > index) && (endB > index) && (a[--endA] === b[--endB])); // Scan end
return { index, before: a.substring(index, endA), after: b.substring(index, endB) };
}
console.log("Example:", diff("lebronjames", "lebronnjames"));
console.log("Same:", diff("This is a test", "This is a test"));
console.log("Mid:", diff("This is a test", "This is not a test"));
console.log("Start (A):", diff("**This is a test", "This is a test"));
console.log("Start (B):", diff("This is a test", "**This is a test"));
console.log("End (A):", diff("This is a test!!", "This is a test"));
console.log("End (B):", diff("This is a test", "This is a test!!"));
我获取两个字符串之间差异的方法。希望这个函数能帮助到某些人:
function getDifferences(a, b){
let result = {
state : true,
diffs : []
}
if(a===b) return result;
result.state = false;
for (let index = 0; index < Math.max(a.length,b.length); index++) {
if (a[index] !== b[index]) {
result.diffs.push({index: index, old: a[index], new: b[index]})
}
}
return result;
}
b
比 a
更长,那么在 a
结束后的所有差异都不会被推入到 result.diffs
数组中吗? - Brandon Tom