逐个字符地比较javascript中的两个字符串。 [英] Compare two strings in javascript character by character.

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

问题描述




i希望逐个字符地比较javascript中的两个字符串。

例如: -



i有两个字符串operations和opeerationsd。。我想用javascript比较这两个字符串。然后提醒opeerationsd。中的角色,即'e'和'd'。

警告第二个字符串中的不同字符。





谢谢..

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..

推荐答案

不幸的是,这个问题虽然简单,但是没有明确的定义。 (顺便说一下,你需要明白,给出一个例子并不意味着要求行为的定义;例子只能说明它。)



首先,当你拥有所有的时候在编辑输出上,没有关于哪些字符被编辑的信息,它已经丢失了。说,你添加一个字符,然后删除它(我知道你不需要检测它,它只是例如)。然后,如果捕获文本已更改事件,则会检测到它,但无法通过比较线条来查看差异。对于更为重要和现实的例子,请考虑这一点:原始:一些正确的单词,关于输出:令人敬畏的不正确的世界,所以让我把变化大写:AWEsome INcorrect worLds。事实上,有人把现有的片段一些,正确,烦恼和ds(请注意,即使你使用过的话,其中两个也不是你可以从英语词典中获取的可识别世界),所以你会需要首先在原始文本中识别这些片段以显示差异。



这不是一个小问题;我们仍然没有定义我们想要达到的目标,甚至可能是模糊要求的问题...



想要了解更有效的方法,你可以熟悉差异算法: http://en.wikipedia.org/wiki/Diff [ ^ ]。



它们是不同的:它们每行显示数据,但有助于识别匹配和不同的片段,并以一种人类读者可理解的方式显示差异。我不确定在单线水平上做这件事是否合理(我会说不是),但至少你可以理解这个问题。



-SA
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


这是javascript插件,可能对你有帮助。它是免费的。



Javascript Diff算法



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



我希望这会对你有所帮助。
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.


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

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