Click here to Skip to main content
14,271,845 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi
i want to compare two strings in javascript character by character.
for example:-

i have two string "operations" and "opeerationsd.". i want to compare these two string using javascript. then alert the characters in "opeerationsd.", that is 'e' and 'd'.
alert the different characters in second string.


thanks..
Posted
Updated 20-Sep-13 4:47am
v5
Comments
CodeBlack 20-Sep-13 9:45am
   
what you want as an output ? I mean if strings are not compared than what should be the output of javascript method ?
hasbina 20-Sep-13 9:48am
   
sorry sir..updated the question.
NotPolitcallyCorrect 20-Sep-13 9:54am
   
Okay and the question and/or problem with the code that you have written is what?
Sergey Alexandrovich Kryukov 20-Sep-13 10:17am
   
This is not as trivial as it may seem — please see my answer.
—SA
phil.o 20-Sep-13 10:00am
   
Sure, feel free to do it, you have our permission.
What have you tried so far?
Sergey Alexandrovich Kryukov 20-Sep-13 10:17am
   
This is not as trivial as it may seem — please see my answer.
—SA
phil.o 20-Sep-13 10:38am
   
You're right, not so trivial. I guess I had too much computer-related problems for today ; I'd better leave and have some fun, as I'm lackig of required patience to help efficiently.
hasbina 20-Sep-13 10:45am
   
sir, updated the question
Rate this:
Please Sign up or sign in to vote.

Solution 1

Unfortunately, the problem, despite illusionary simplicity, is not well defined. (By the way, you need to understand that giving an example does not mean definition of required behavior; examples can only illustrate it.)

First, when all you have on the output of editing, there is no information on what characters are "edited", it is already lost. Say, you add a character but then remove it (I understand that you don't need to detect it, it's just for example). Then, if you capture the "text changed" event, you detect it, but cannot see the difference by comparing lines. For more essential and realistic example consider this: original: "some correct words", on output: "awesome incorrect worlds", so let me capitalize the "changes": "AWEsome INcorrect worLds". In fact, one took the existing fragments "some", "correct", "wor" and "ds" (note that two of them are not recognizable worlds you could took from an English dictionary even if you used one), so you would need first to "recognize" these fragments in the original text to show the "differences".

This is not a trivial problem; and we still don't have a definition of what we want to achieve, it might be even a problem with ambiguous requirements…

To get some idea on more productive approach, you can get familiar with such thing as "diff" algorithms: http://en.wikipedia.org/wiki/Diff[^].

They are different: they display data per line, but the help to identify matching and different fragment and display in the way to show "differences" in a way comprehensible to a human reader. I an not sure it makes any sense to do it at the level of single line (I would say not), but at least you can get to understanding of the problem.

—SA
   
Comments
hasbina 20-Sep-13 10:47am
   
sorry sir..
my aim was only to alert the different characters in second string.

Thanks...
Sergey Alexandrovich Kryukov 20-Sep-13 11:32am
   
No need to be sorry; you did not do anything wrong; this is a normal process of getting help.
I would advise not to analyze all the differences, just select the first different character, maybe. In this case, what's the problem?
You 1) find which line is shorter, 2) loop from 0 to the shortest length minus 1, compare characters one-by-one, 3) select the first mismatch.

I only don't understand why entering already existing strings? Is it some kind of typing training, or what?
By the way, it's always good to explain your ultimate goals when asking question.

—SA
hasbina 20-Sep-13 12:09pm
   
sir,

my actual aim is to underline only added characters when i edited the data. i am using diff algorithm in my code with little changes in the algorithm.

thanks..thanks a lotss sir..
Sergey Alexandrovich Kryukov 20-Sep-13 13:30pm
   
Great. So what's remaining problem? How to underline? You can underline with the "u" tag, but you cannot modify the text which in the input control or text area. You can only do it in HTML.
—SA
hasbina 21-Sep-13 8:33am
   
thanks sir...thanks a lot.....
Sergey Alexandrovich Kryukov 21-Sep-13 21:36pm
   
Sure. Will you accept the answer formally?
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

Here is the javascript plugin, where it may be helped to you.It's free.

Javascript Diff Algorithm

http://ejohn.org/projects/javascript-diff-algorithm/#postcomment[^]

I hope this will help to you.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100