Javascript比较字符串并获得结束差异

Ian*_*ugh 16 javascript string

我有两个字符串.字符串 - 答:"快速的棕色狐狸"字符串B:"快速的棕色狐狸跳过懒狗."

字符串B将始终包含字符串A逐字.永远不会有"快速黑狐狸"或"快速而快速的棕色狐狸".

如何获得差异的"字符串C""跳过懒狗"?

Mat*_*vic 17

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.
Run Code Online (Sandbox Code Playgroud)


Gho*_*Man 12

请参阅下面的基本示例.这可以很容易地针对不同的行为进行修改/扩展.

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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


chi*_*him 7

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');
Run Code Online (Sandbox Code Playgroud)

注意:此答案解决了查找额外查询参数(基于另一个查询字符串)的问题,并且不是OP的确切答案.

  • 这是一个公平的评论@Michael我没有回答原来的问题,但发布了我当时遇到的问题的解决方案.我想我一定是这样做的,因为这是我试图解决的问题,这是comu的答案帮助了我,所以我发布了我用过的代码.我会更新我的答案,以反映它没有完全回答这个问题. (2认同)
  • 鬼人的答案应该是公认的答案. (2认同)
  • 我赞扬您的StackOverflow完整性.:-) (2认同)

com*_*omu 6

您需要将每个单词交叉检查到另一个单词.

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(" "));
Run Code Online (Sandbox Code Playgroud)

  • "字符串B将始终包含字符串A逐字"表示"不要逐字检查" (2认同)

kc2*_*001 6

如果“字符串 B 将始终包含字符串 A”,以下内容还不够吗?

var c = b.replace(a, "");
Run Code Online (Sandbox Code Playgroud)