0

Here, i don't understand when i compare string ".doc" with string ".exe", Compare method gives -1 value and false as a return. Where it have to return True and 1 value as a return just because i set it as "==1".

When i compare string ".exe" with string ".txt" and string ".exe" it's give valid return value.

Can anyone tell me what actually happened here?

int nDocValue = string.Compare(".doc", ".exe");          
Console.WriteLine(nDocValue);                            // Will display -1
Console.WriteLine(string.Compare(".doc", ".exe") == 1);  // False

int nTxtValue = string.Compare(".txt", ".exe");          
Console.WriteLine(nTxtValue);                            // Will display 1
Console.WriteLine(string.Compare(".txt", ".exe") == 1);  // True

int nExeValue = string.Compare(".exe", ".exe");          
Console.WriteLine(nExeValue);                            // Will display 0
Console.WriteLine(string.Compare(".exe", ".exe") == 1);  // False
Qwertiy
  • 19,681
  • 15
  • 61
  • 128
  • Exactly what was expected and described in the docs? [String.Compare](https://msdn.microsoft.com/en-us/library/84787k22(v=vs.110).aspx) returns 0 for equal, -1 for smaller, 1 for larger. That's how all implementations of [IComparable.Compare](https://msdn.microsoft.com/en-us/library/system.icomparable(v=vs.110).aspx) work. – Panagiotis Kanavos Mar 10 '17 at 09:24
  • You have plenty of answer. If you want to check if they are different, you have to check that the `Compare` returns something different from `0` – Pikoh Mar 10 '17 at 09:25
  • sorry i thought true if equals and false if not. My bad. –  Mar 10 '17 at 09:32

4 Answers4

4

From MSDN String.Compare (String strA, String strB) see Return Value section:

  • Less than zero - strA precedes strB in the sort order.
  • Zero - strA occurs in the same position as strB in the sort order.
  • Greater than zero - strA follows strB in the sort order.

Here is the sort order of your extensions strings:

.doc
.exe
.txt

In your first example ".doc" precedes ".exe" and you have less than zero return value.

NOTE that specification does not say that greater than zero is equal to 1. You should not rely on that value and compare with == 1. You should compare with zero instead. Here is different comparisons which you might use

  • result < 0 - check if strA goes before strB in the sort order: string.Compare(".doc", ".exe")
  • result == 0 - check if strA equals strB: string.Compare(".exe", ".exe")
  • result > 0 - check if strA goes after strB in the sort order: string.Compare(".txt", ".exe")
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
3

Have you read the MSDN page about string.Compare()? It returns a value < 0 if the first string is smaller (in a Lexicographical order) to the second one, a value > 0 if it is greater, 0 if they are equal.

doc < exe < txt

Note that if you only need to check for equality, then using the == operator or the string.Equals() is better.

bool bDocValue = string.Equals(".doc", ".exe"); // False
bool bDocValue2 = ".doc" == ".exe"; // False
xanatos
  • 109,618
  • 12
  • 197
  • 280
3

You must compare only with zero:

  • Any negative value means that the first string is less

    .doc
    .exe
     ^______ `d` goes before `e`
    
  • Zero means that the strings are equal

    .exe
    .exe
        ^___ strings are equal
    
  • Any positive value means that the first string is greater

    .txt
    .exe
     ^______ `t` goes after `e`
    
Qwertiy
  • 19,681
  • 15
  • 61
  • 128
0

The .Compare()does not only check for equality. Check for greater, minor or equal. Is often used for sorting algorythms. Check the interface definition.

https://msdn.microsoft.com/en-us/library/system.collections.icomparer(v=vs.110).aspx

Cleptus
  • 3,446
  • 4
  • 28
  • 34