Javascript Javascript比较字符串并获得最终差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8024102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript compare strings and get end difference
提问by Ian McCullough
I have two strings. String A: "The quick brown fox" String B: "The quick brown fox jumps over the lazy dog."
我有两个字符串。字符串 A:“敏捷的棕色狐狸” 字符串 B:“敏捷的棕色狐狸跳过懒狗。”
String B will always contain string A verbatim. There will never be a "quick black fox" or a "quick and speedy brown fox".
字符串 B 将始终逐字包含字符串 A。永远不会有“行动敏捷的黑狐”或“行动迅速的棕狐”。
How do a get a "String C" of the difference "jumps over the lazy dog."?
如何获得“跳过懒狗”的差异的“字符串C”?
回答by Mateja Petrovic
const A = "The quick brown fox"
const B = "The quick brown fox jumps over the lazy dog."
const diff = (diffMe, diffBy) => diffMe.split(diffBy).join('')
const C = diff(B, A)
console.log(C) // jumps over the lazy dog.
回答by Ghost-Man
See the basic example below. This can easily be modified/extended for different behaviour.
请参阅下面的基本示例。这可以很容易地针对不同的行为进行修改/扩展。
var stringA = document.getElementById('string_a').textContent,
stringB = document.getElementById('string_b').textContent,
firstOccurrence = stringB.indexOf(stringA);
if(firstOccurrence === -1)
{
alert('Search string Not found');
}
else
{
var stringALength = stringA.length;
var newString;
if(firstOccurrence === 0)
{
newString = stringB.substring(stringALength);
}
else
{
newString = stringB.substring(0, firstOccurrence);
newString += stringB.substring(firstOccurrence + stringALength);
}
document.getElementById('diff').textContent = newString;
}
<p>String A: <span id="string_a">The quick brown fox</span></p>
<p>String B: <span id="string_b">The quick brown fox jumps over the lazy dog</span></p>
<hr/>
<p>Difference: <span id="diff"></span></p>
回答by chim
comu's answer as a function...
comu 的答案是一个函数......
function compareString( s1, s2, splitChar ){
if ( typeof splitChar == "undefined" ){
splitChar = " ";
}
var string1 = new Array();
var string2 = new Array();
string1 = s1.split( splitChar );
string2 = s2.split( splitChar );
var diff = new Array();
if(s1.length>s2.length){
var long = string1;
}
else {
var long = string2;
}
for(x=0;x<long.length;x++){
if(string1[x]!=string2[x]){
diff.push(string2[x]);
}
}
return diff;
}
compareString( "?Yo=dude", "?Yo=Dude&do=roby", "&" ).join('\n');
compareString( "?Yo=Dude", "?Yo=Dude&do=roby", "&" ).join('\n');
Note: this answer solves the issue of finding extraquery parameters (based on another query string), and is not an exact answer for the OP.
注意:此答案解决了查找额外查询参数(基于另一个查询字符串)的问题,并不是 OP 的准确答案。
回答by comu
You need to cross check each word to the other one.
您需要将每个单词与另一个单词进行交叉检查。
var s1 = "The quick brown fox",
s2 = "The quick brown fox jumped over the fence",
string1 = new Array(),
string2 = new Array(),
diff = new Array(),
longString;
string1 = s1.split(" ");
string2 = s2.split(" ");
if (s1.length > s2.length) {
longString = string1;
} else {
longString = string2;
}
for (x = 0; x < longString.length; x++) {
if (string1[x] != string2[x]) {
diff.push(string2[x]);
}
}
document.write("The difference in the strings is " + diff.join(" "));
回答by kc2001
If "String B will always contain string A verbatim", won't the following suffice?
如果“字符串 B 将始终包含字符串 A”,以下内容还不够吗?
var c = b.replace(a, "");
回答by akshay
Check out this site for a wonderful API to see the difference between strings: google-diff-match-patchYou might need to check the UI according to your need though.
查看此站点的精彩 API 以查看字符串之间的区别:google-diff-match-patch 不过,您可能需要根据需要检查 UI。
回答by Pim H
I found the simple way.
我找到了简单的方法。
Here is the code:
这是代码:
var t1 = "The quick brown fox";
var t2 = "The quick brown fox jumps over the lazy dog.";
var indxFoundStart = t2.search(t1); //first index of the position of t1 found in t2 (not use but you can print for sure)
var indxFoundEnd = t1.length; //last index of t1 found in t2
var textCompareLen = t2.length; // length of the longer string
// You can add the condition to verify which text is shorter, which text is longer if you do not want to fixed in t1 and t2
document.write("difference string is: " + t2.substring(indxFoundEnd, textCompareLen));
I have try with 2 sample, the text with space and the text without space and it work fine.
我尝试了 2 个示例,带空格的文本和不带空格的文本,效果很好。
Absolutely, I have try with your example, and it work fine too.
当然,我已经尝试过你的例子,它也很好用。