Home > Software engineering >  Find out the difference between two strings to update new content
Find out the difference between two strings to update new content

Time:05-09

I am doing a small tool that reads text from a TXT file and updates it to a HTML page, using Jquery PrependTo a #DIV_ID (bottom-up update, new content at the top).

I have created an Autohotkey Chrome AHK script that gets data from some smart home sensors... and this script can communicate with Chrome via console commands. It continuously reads a local TXT file every 0.5 seconds and update new content to the HTML page.

The problem is, I want to update Only new content, not the whole page, I'd love to prepend new content to the top of the page.

Please tell me a simple way using Javascript to Match from the end to the beginning of two strings, stop at the first difference, and then get all the characters from there to the beginning of the string.

string1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry"

string2 = "Hello Lorem Ipsum abcd... I want to get these texts. Lorem Ipsum is simply dummy text of the printing and typesetting industry"

Should it be done with regular expression? Or I have to split these strings in a For loop? that would be poor performance.

I'd really appreciate it!

CodePudding user response:

check with function endsWith then do replace

var string1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry"

var string2 = "Hello Lorem Ipsum abcd... I want to get these texts. Lorem Ipsum is simply dummy text of the printing and typesetting industry"

var diff = ''

if (string2.endsWith(string1)) {
  diff = string2.replace(string1, '')
}

console.log(diff)

CodePudding user response:

I firgered out a traditional method, not sure if this is a good way

var oldTranscript = "hey how are you";
var newTranScript = "hey Micheal hi how are you";
var tobeUpdate = "";
x = oldTranscript.length - 1;
for (var i = newTranScript.length - 1; i >= 0; --i) {

  if (newTranScript.charAt(i) != oldTranscript.charAt(x)) {
   tobeUpdate  = newTranScript.charAt(i);
  } 
  --x;
}
console.log(tobeUpdate.split('').reverse().join(''));

  • Related