1

I am wondering if there is a way in JavaScript by which I can detect which part of my Strings makes them different from each other.

Let's say I have three strings as follows:

String1 = "Java1String"
String2 = "Java2String"
String3 = "Java3String"

If I choose my first String as a main one, the part which makes it different from the others is 1.

Is there any way using either JavaScript or jQuery by which I can find this part?

7
  • 1
    You're looking for en.wikipedia.org/wiki/Levenshtein_distance Commented Sep 7, 2014 at 2:31
  • @SLaks While related, I don't see how that gives the OP their answer Commented Sep 7, 2014 at 2:33
  • 11
    Are you going to show us your attempts to write the code or just let us do the work for you? Do you have any limitations on the inputs? Are they always strings? Always the same length? Always three of them? Commented Sep 7, 2014 at 2:34
  • 1
    You need to look for "diff" algorithms. The solution will have nothing whatsoever to do with jQuery. Commented Sep 7, 2014 at 2:59
  • My inputs always are String and there is no limitation. Actually I am parsing a big database of Strings and this is only a part of my work. I just needed to make sure if it is possible and see if there is something which can do that for me directly. Thanks to all. Commented Sep 7, 2014 at 3:10

3 Answers 3

-1
var String1 = "Java1String",
String2 = "Java2String",
String3 = "Java3String";

var j = 0;

for(var i = 0; i < String1.length; i++){
    if(String1.charAt(i) != String2.charAt(j))
        alert(String1.charAt(i) +"  !=  "+ String2.charAt(j));
        j++;
}

You can check out a demo of this code with this jsfiddle.

Sign up to request clarification or add additional context in comments.

3 Comments

Your code alerts 4, which is ofcourse not suppose to happen.
i have updated the code.Now Check the link in the post it will show you characters which are Different.
previous code was alerting the position of the different character @chipChocolate.py
-1
You can compare two strings like this. This will give you the characters which are different.


var String1 = "Java1String",
String2 = "Java2String",
String3 = "Java3String";

var j = 0;

for(var i = 0; i < String1.length; i++){
    if(String1.charAt(i) != String2.charAt(j))
        alert(String1.charAt(i) +"  !=  "+ String2.charAt(j));
        j++;
}

You can check out Demo of this code on this link

http://jsfiddle.net/enL9b3jv/1/

Comments

-3

The naive solution would be to convert each string into an array and iterate over the arrays, compare the character at each index until you find an index that doesn't match, and then write that index to a variable. Below is a Jsbin that does just that, but just as DevIshOne states, there are many questions to answer here...

http://jsbin.com/dugovemoxupu/1/edit

3 Comments

You don't need to convert a JS string to an array to iterate over the characters. "test"[2] will return "s" and .length works fine on strings.
Updated the JsBin to not use the array, but I don't see how that affects the quality of the answer.
Maybe it's because you linked to code without posting it? Not sure, I'm not a down-voter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.