Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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.
[no name] 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?

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
 
Share this answer
 
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.....
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900