String Comparison And Alphabetic Order of Individual Characters

后端 未结 4 1142
死守一世寂寞
死守一世寂寞 2021-02-18 23:06

I have a question related to string comparison vs. character comparison.

Characters > and 0 (zero) have following decimal values 62

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-19 00:05

    When you compare the characters '>' and '0', you are comparing their ordinal values.

    To get the same behaviour from a string comparison, supply the ordinal string comparison type:

      Console.WriteLine(string.Compare(">", "0", StringComparison.Ordinal));
      Console.WriteLine(string.Compare(">", "0", StringComparison.InvariantCulture));
      Console.WriteLine(string.Compare(">", "0", StringComparison.CurrentCulture));
    

    The current culture is used by default, which has a sorting order intended to sort strings 'alphabetically' rather in strictly lexical order, for some definition of alphabetically.

提交回复
热议问题