0

I use String.Compare like below sample

static void Main(string[] args)
    {
        int result = String.Compare("A", "a");
        Console.Write(result);
        Console.ReadKey();
    }

but , when run the program , result is 1 . but i except result will be -1 . i use vs 2013 .

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • I use C# language . (vs2013) – hamidreza7 Aug 09 '14 at 18:38
  • 1
    I bet hjpotter92 was talking about locales. Anyhow in the first edition of the question it was "A" and "b", now it's "A" and "a". – GSerg Aug 09 '14 at 18:39
  • Yes , now is right . but when i run program , i except result will be -1 but result is 1 ?! why ? – hamidreza7 Aug 09 '14 at 18:42
  • (specifically http://stackoverflow.com/a/529430/11683) – GSerg Aug 09 '14 at 18:47
  • I am also assuming it has to do with locales ... in dotnetfiddle it yields 1 https://dotnetfiddle.net/U9XqkJ – okaram Aug 09 '14 at 18:51
  • @okaram It yields 1 for the OP too. – GSerg Aug 09 '14 at 18:55
  • 1
    In addition to reading through duplicate question, please also carefully review [Best Practices for Using Strings](http://msdn.microsoft.com/en-us/library/dd465121%28v=vs.110%29.aspx#the_details_of_string_comparison) on MSDN (which linked from `String.Compare` documentation for your convenience). – Alexei Levenkov Aug 09 '14 at 19:23

1 Answers1

-2

Your expectation is wrong. String.Compare(str1, str2) if the str1 is greater than str2 then the result is greater than zero and if the str1 is less than str2 then the result is less than zero and if the str1 is equals str2 then the result is zero

  • Exactly. The OP knows that and rightfully expects "a" to be greater than "A". But it is the other way round. – GSerg Aug 09 '14 at 19:14