tags:

views:

1028

answers:

4

I have two strings read in from textfiles to compare and when I try to compare these files with winmerge or pspad, they both show as the same text strings. If I compare them with the following function, it fails:

string string1 = File.ReadAllText(@"c:\file1.txt");
string string2 = File.ReadAllText(@"c:\file2.txt");    
bool stringMatch = false;
if (string1.Equals(string2, StringComparison.InvariantCulture)){
    stringMatch = true;
}
//stringMatch is false here

After some searching it seems to be that a " and ' are different:

Content of file1.txt: é"'(§è!çà)- 
Content of file2.txt: é”’(§è!çà)-

Any way I can properly compare these two strings and match those " & ' characters?

+1  A: 

You could convert them both to byte[] using the methods under System.Text.Encoding and then compare the byte[] arrays

Eoin Campbell
+1  A: 

It looks like you want to use the overload which takes StringComparison. I'd guess given the current senario you want the "Ordinal" value but you may want one of the others depdending on what you are doing.

http://msdn.microsoft.com/en-us/library/system.stringcomparison.aspx

Saint Gerbil
A: 

Well, you don't have the .NET strings in WinMerge or pspad, so something could well be going wrong while decoding. You need to explain your exact scenario:

  • Is the data in a file (hence WinMerge/pspad)?
  • How are you loading the file in .NET?
  • How are you loading the file in WinMerge etc?

EDIT: Okay, based on the comment - what is the encoding of the file meant to be? Are you specifying it in WinMerge anywhere? .NET will be using UTF-8 (because you haven't specified any other encoding).

Jon Skeet
1) The data is in a *.txt file2) Files is being read with string string1 = File.ReadAllText(@"c:\file1.txt")3) I'm just opening file1 and use the "Text differences with file2.txt" option.
Carra
+1  A: 

After reading "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)" you should be well equipped to solve your problem yourself.

David Schmitt