比较js中的两个字符串并找到差异 [英] Compare two string in js and find difference

查看:2376
本文介绍了比较js中的两个字符串并找到差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

how to compare two strings purely, and provide specific result such as highlight extra word, wrong word & skip word in 2nd string. for eg.

var x = "This is the first original string in javascript language." </br>
var y = "This is not the first org string in language."

diff =  wrong word ="org"<br>
        Extra word ="not"<br>
        skip word ="javascript"




<pre> <script lang="Javascript">
//here is slice of my code but in some case my program fails
  var x = "here is some value of string";
  var y = "here is the some val string";
  var k=0; 
  var SkipWrd="";
 for(var i=0; i<y.length;i++){ 
    var sktmp="";
    var swtmp=0;
     for(var j=0; j<=2;j++) { 
         if(x[k]!="undefined"){ 
             if(y[i]==x[k+j]){  

                 SkipWrd+=sktmp;
                 skip+=swtmp;
                 H_wrd +=  typ_wrd[i]+" ";
                 org_para+= sktmp+x[k+j]+" ";

                 k+=j+1;
                 break;
             }
            else{
             sktmp+= "<mark>"+ x[k+j]+" "+ "</mark>";
             swtmp++; 
             if(j==2 && y[i]!=x[k+j]){
                 err++;
                 Err_Wrd+=y[i]+" ";
                 H_wrd += "<span id='H-Err'>" + typ_wrd[i] + "</span> ";
                 org_para+="<span id='O-Err'>" +x[k]+" "+ "</span> ";

                 k++;
             }
            }
        } 
     }
 }





我尝试了什么:



如果第二个字符串中包含额外的单词,那么将字符串计为错误的单词。



What I have tried:

if extra word in included in 2nd string then rest string count as a wrong word.

推荐答案

您可以使用较小的字符数计数字符串,将其拆分为单词表格。

之后,您循环遍历表格,对于您测试的每个单词,如果它存在于另一个短语中,它是索引,...
You can take the string with smaller caracters count, split it into table of words.
After that you loop throught the table and for every word you test if it exist in the other phrase, it's index, ...


引用:

如果包含额外的单词在第二个字符串然后休息字符串计为一个错误的单词。

if extra word in included in 2nd string then rest string count as a wrong word.



我猜你的算法已经过度简化。

我认为问题是优化你希望减少更改次数的问题,处理比你做的更复杂。

调试器将显示你的代码正在做什么,它应该有助于你理解它在哪里错了。

-----

您的代码没有按照您的预期行事,您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在做什么和你的任务是与它应该做的比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到bug,它只是帮助你告诉你发生了什么。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

JavaScript调试 [ ^ ]

Chrome DevTools &NBSP; |&NBSP;网络  |  Google Developers [ ^ ]

这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


I guess your algorithm is over simplified.
I see the problem as an optimization problem where you want to minimize the number of changes, and the handling is more complicated than what you did.
The debugger will show what you code is doing, it should help you to understand where it goes wrong.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
JavaScript Debugging[^]
Chrome DevTools  |  Web  |  Google Developers[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于比较js中的两个字符串并找到差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆