📅  最后修改于: 2020-10-31 03:17:36             🧑  作者: Mango
C#Compare()方法用于按字典顺序比较第一个字符串和第二个字符串。它返回一个整数值。
如果两个字符串相等,则返回0。如果第一个字符串大于第二个字符串,则返回1,否则返回-1。
s1==s2 returns 0
s1>s2 returns 1
s1
public static int Compare(String first, String second)
public static int Compare(String, Int32, String, Int32, Int32)
public static int Compare(String, Int32, Int32, String, Int32, Boolean)
public static int Compare(String, Boolean, Int32, Int32, String, Int32, CultureInfo)
public static int Compare(String, CultureInfo, Int32, Int32, String, Int32, CompareOptions)
public static int Compare(String, Int32, Int32, String, Int32, StringComparison)
public static int Compare(String, String, Boolean)
public static int Compare(String, String, Boolean, CultureInfo)
public static int Compare(String, String, CultureInfo, CompareOptions)
public static int Compare(String, String, StringComparison)
第一:第一个参数表示字符串这是与第二字符串进行比较。
第二:第二个参数表示的字符串,是与第一字符串进行比较。
它返回一个整数值。
using System;
public class StringExample
{
public static void Main(string[] args)
{
string s1 = "hello";
string s2 = "hello";
string s3 = "csharp";
string s4 = "mello";
Console.WriteLine(string.Compare(s1,s2));
Console.WriteLine(string.Compare(s2,s3));
Console.WriteLine(string.Compare(s3,s4));
}
}
输出:
0
1
-1