Skip to content

Get numbers of difference in two strings javascript

i have two string

const string1 = "usa(Country), with concealed(O), zipper(Closure)"
const string2 = "usa(Country), with(O), concealed zipper(Closure)"

I want to find diff in these two string for example in this scenario it is 2


I’m presuming the strings need to be compared based on the comma separations, but to be fair there isn’t enough details in the question…

Split the strings by comma

// split function will be an array of strings
const split = (str, delimiter) => str.split(delimiter)

Apply the above function to both strings, and compare the results

// this uses lodash
// result will be an array, get the length
const result = _.difference(split(string1, ','), split(string2, ',')

If you’re not going to use lodash, you can look up the vanilla JS implementation of difference

If order is important, you may need _.xor, but you can determine that with testing