我们可以使用 C# 比较两个 JavaScript 文件吗?
我尝试比较两个文本文件。如果它们包含相同的数据,但即使有一个空格的差异,结果也会显示为“不同”。
谁能告诉我如何使用 C# 比较两个 JavaScript 文件?
I have tried comparing two text files. If these contain the same data but there is a difference of even one space the result is showing as ‘different’.
Can anyone tell me how to compare two JavaScript files using C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 JavaScript 是容忍空格的(只要语法正确,就可以容忍任意数量的空格),因此如果您想要比较除空格以外的所有内容,最简单的做法就是进行正则表达式替换:
运行此命令两者都归档并比较结果;它用单个空格替换任何标准空白字符序列(空格、制表符、回车符、垂直制表符等)。然后,您可以与 Equals 进行比较(根据需要,区分大小写或不区分大小写)。
当然,空格在字符串中很重要,因此假设所有比较文件中的字符串处理不太依赖空格。
然而,两个截然不同的代码文件可能会产生相同的效果,因此,如果这就是您所追求的,那么您的工作就很困难。
Since JavaScript is whitespace tolerant (tolerates any amount of whitespace as long as the syntax is correct), the simplest thing to do if you want to compare everything but the whitespace is to regex-replace:
Run this on both files and compare the results; it replaces any sequence of standard whitespace characters (space, tab, carriage return, vertical tab etc.) with a single space. You can then compare with Equals (case sensitive or not, as you require).
Of course, whitespace IS significant inside strings, so this assumes the string handling in all the compared files does not rely on whitespace too much.
However two very different code files can have the same effects, so if that's what you're after you have a hard job ahead of you.
您只需要知道它们是否完全相同?如果是这样,您可以将它们加载到内存中并比较 .length() 属性...
Do you just need to know if they are exactly the same? If so you could just load them into memory and compare the .length() property...
从技术上讲,如果一个文件包含额外的空格,那么它们就不是“相同的”。我首先会比较长度,如果长度不匹配,则需要进行逐字节比较。如果您想删除多余的空格,您可能需要首先对两个文件的内容执行诸如 Trim() 之类的操作。
下面是一个旧的 MS 帖子的链接,描述了如何创建文件比较函数:
http://support.microsoft. com/kb/320348
Technically, if one file contains an extra space they aren't "the same". I would first compare the lengths and if those don't match you'll need to do a byte by byte comparison. If you want to remove extra spaces you'll probably want to do something like a Trim() on the contents of both files first.
Here's a link to an old MS post describing how to create a file compare function:
http://support.microsoft.com/kb/320348