'compare strings and get end difference

I have two strings.
String A: "The quick brown fox"
String B: "The quick brown fox jumps over the lazy dog."

String B will always contain string A verbatim. There will never be a "quick black fox" or a "quick and speedy brown fox".

How do I get a "String C" of the difference "jumps over the lazy dog."?



Solution 1:[1]

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.

Solution 2:[2]

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>

Solution 3:[3]

comu's answer as a function...

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 extra query parameters (based on another query string), and is not an exact answer for the OP.

Solution 4:[4]

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(" "));

Solution 5:[5]

If "String B will always contain string A verbatim", won't the following suffice?

var c = b.replace(a, "");

Solution 6:[6]

Check out this site for a wonderful API to see the difference between strings: google-diff-match-patch You might need to check the UI according to your need though.

Solution 7:[7]

const str1 = "The quick brown fox jumps over the lazy dog.";
const str2 = "he quick brown fox";

res = Str1.split(str2).join('');

Solution 8:[8]

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.

Absolutely, I have try with your example, and it work fine too.

Solution 9:[9]

You can just use String.prototype.replace() :

var string1 = "The quick brown fox"
    
var string2 = "The quick brown fox jumps over the lazy dog."
    
var diff = ''
    
diff = string2.replace(string1, '')
    
console.log(diff)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Rory O'Kane
Solution 3
Solution 4 Tushar Walzade
Solution 5 kc2001
Solution 6 akshay
Solution 7 greybeard
Solution 8 Pim H
Solution 9 Taher ben abdallah