比較字串CompareTo的用法及注意
阿新 • • 發佈:2019-02-16
CompareTo用法
static void Main(string[] args) { string str = "1"; string str2 = "2"; int a=str2.CompareTo(str); //str2與compareTo(引數str)比較。2大於1,返回值為1。 Console.WriteLine(a.ToString()); if (a<0) { Console.WriteLine("true"); } else { Console.WriteLine("wrong"); } Console.ReadKey(); } 注意:
返回值:
如果該 str2 等於 CompareTo(str)中的str引數,則返回 0 值;如果str2 在數字上小於 CompareTo(str)中的str 引數,則返回值-1;如果 str 在數字上大於CompareTo(str)中的str 引數,則返回整數1。