0

2 つのテキスト ファイルを比較してみました。これらに同じデータが含まれていても、スペースが 1 つでも異なる場合、結果は「異なる」と表示されます。

C# を使用して 2 つの JavaScript ファイルを比較する方法を誰か教えてもらえますか?

4

3 に答える 3

1

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:

Regex _r = new Regex(@"\s+", RegexOptions.Compiled);
string result = _r.Replace(value, " ");

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.

于 2010-09-27T13:18:52.273 に答える
0

それらがまったく同じであるかどうかを知る必要がありますか?もしそうなら、それらをメモリにロードして、.length()プロパティを比較することができます...

于 2010-09-27T12:58:54.353 に答える
0

技術的には、1 つのファイルに余分なスペースが含まれている場合、それらは「同じ」ではありません。最初に長さを比較し、それらが一致しない場合は、バイトごとに比較する必要があります。余分なスペースを削除したい場合は、最初に両方のファイルの内容に対して Trim() のようなことをしたいと思うでしょう。

ファイル比較関数の作成方法を説明している古い MS 投稿へのリンクを次に示します。

http://support.microsoft.com/kb/320348

于 2010-09-27T13:12:59.807 に答える