我有两个字符串,第二个与第一个相同,但在字符串中的某个位置添加了三个字符(三个相同的字符)
下面的例子:
string1 = "hello"
string2 = "aaahello"
// => 'a'
字符可以位于字符串中的任何位置。 另一个例子
string1 = "abcde"
string2 = "2db2a2ec"
// => '2'
还有一个警告,添加的字符也可能存在于原始字符串中
string1 = "aabbcc"
string2 = "aacccbbcc"
// => 'c'
我已经尝试了以下操作,但这并没有给我添加到第二个字符串中的三个相同的字符。我不知道如何找到三个添加的相同字符:
function addedChar(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;
}
最佳答案
这个怎么办?这只是一种方式,但您可以采用两种方式。
string1 = "aabbcc";
string2 = "aacccbbcc";
diff = string2;
for(let i = 0; i < string1.length; i++) {
const char = string1[i];
diff = diff.replace((new RegExp(`${char}`)), '');
}
//ccc
console.log(diff);
关于javascript - 查找字符串js中添加的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45287031/