System.String.Compare 方法 (String, String)

方法描述

比较两个指定的 String 对象,并返回一个整数,指示二者在排序顺序中的相对位置。

语法定义(C# System.String.Compare 方法 (String, String) 的用法)

public static int Compare(
	string strA,
	string strB
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
strA System-String 要比较的第一个字符串。
strB System-String 要比较的第二个字符串。
返回值 System.Int32 一个 32 位带符号整数,指示两个比较数之间的词法关系。 值 Condition 小于零 strA 小于 strB。 零 strA 等于 strB。 大于零 strA 大于 strB。

提示和注释

该比较使用当前区域性获取区域性特定的信息,例如大小写规则和各个字符的字母顺序。 例如,区域性可以指定将某些字符的组合视为单个字符,或者用特殊方法比较大写和小写字符,或者字符的排序顺序取决于它前面或后面的字符。

使用字排序规则执行比较。 有关字、字符串和顺序排序的更多信息,请参见 System.Globalization.CompareOptions。

警告

比较字符串时,您应该调用 Compare 方法,这需要您显式指定该方法使用的字符串比较的类型。 有关更多信息,请参见 在 .NET Framework 中使用字符串的最佳做法。

一个或者两个比较字都可以是 null。 根据定义,任何字符串(包括空字符串 (""))的比较结果都大于 null 引用;两个 null 引用的比较结果为相等。

当发现不相等或已经比较了两个字符串时,比较就会终止。 但是,如果两个字符串一直比较到其中一个字符串的末尾时仍相同,而另一个字符串仍有剩余字符,则认为仍有剩余字符的字符串较大。 返回值为执行最后一次比较所得的结果。

当比较受区域性特定的大小写规则影响时,可能会发生意外结果。 例如,在土耳其语中,下面的示例将产生错误结果,因为土耳其语的文件系统不会对“file”中的字母“i”使用语义大小写规则。

C#

C++

VB

复制

static bool IsFileURI(String path)

{

return (String.Compare(path, 0, "file:", 0, 5, true) == 0);

}

使用序号比较比较路径名与“file”。 执行此操作的正确代码如下:

C#

C++

VB

复制

static bool IsFileURI(String path)

{

return (String.Compare(path, 0, "file:", 0, 5, StringComparison.OrdinalIgnoreCase) == 0);

}

System.String.Compare 方法 (String, String)例子

在下面的示例中,ReverseStringComparer 类说明了如何使用 Compare 方法来计算两个字符串。

using System;
using System.Text;
using System.Collections;

public class SamplesArrayList  {

	public static void Main()  {
		// Creates and initializes a new ArrayList.
		ArrayList myAL = new ArrayList();
		myAL.Add("Eric");
		myAL.Add("Mark");
		myAL.Add("Lance");
		myAL.Add("Rob");
		myAL.Add("Kris");
		myAL.Add("Brad");
		myAL.Add("Kit");
		myAL.Add("Bradley");
		myAL.Add("Keith");
		myAL.Add("Susan");
	
		// Displays the properties and values of	the	ArrayList.
		Console.WriteLine( "Count: {0}", myAL.Count );
		
		PrintValues ("Unsorted", myAL );
		myAL.Sort();
		PrintValues("Sorted", myAL );
		myAL.Sort(new ReverseStringComparer() );
		PrintValues ("Reverse" , myAL );


		string [] names = (string[]) myAL.ToArray (typeof(string));


	}
	public static void PrintValues(string title, IEnumerable	myList )  {
		Console.Write ("{0,10}: ", title);
		StringBuilder sb = new StringBuilder();
		foreach (string s in myList) {
			sb.AppendFormat( "{0}, ", s);
		}
		sb.Remove (sb.Length-2,2);
		Console.WriteLine(sb);
	}
}
public class ReverseStringComparer : IComparer {
   public int Compare (object x, object y) {
	   string s1 = x as string;
	   string s2 = y as string;	  
	   //negate the return value to get the reverse order
	   return - String.Compare (s1,s2);

   }
}

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。