本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

String.Compare 方法 (String, Int32, String, Int32, Int32, Boolean, CultureInfo)

 

比较两个指定的 String 对象(其中忽略或考虑其大小写,并使用区域性特定的信息干预比较),并返回一个整数,指示二者在排序顺序中的相对位置。

命名空间:   System
程序集:  mscorlib(位于 mscorlib.dll)

public static int Compare(
	string strA,
	int indexA,
	string strB,
	int indexB,
	int length,
	bool ignoreCase,
	CultureInfo culture
)

参数

strA
Type: System.String

要在比较中使用的第一个字符串。

indexA
Type: System.Int32

strA 中子字符串的位置。

strB
Type: System.String

要在比较中使用的第二个字符串。

indexB
Type: System.Int32

strB 中子字符串的位置。

length
Type: System.Int32

要比较的子字符串中字符的最大数量。

ignoreCase
Type: System.Boolean

若要在比较过程中忽略大小写,则为 true;否则为 false

culture
Type: System.Globalization.CultureInfo

一个对象,提供区域性特定的比较信息。

返回值

Type: System.Int32

一个整数,指示两个比较字之间的词法关系。

条件

小于零

strA 中的子字符串在排序顺序中位于 strB 中的子字符串之前。

子字符串在排序顺序中出现的位置相同或 length 为零。

大于零

strA 中的子字符串在排序顺序中位于 strB 中的子字符串之后。

Exception Condition
ArgumentOutOfRangeException

indexA 大于 strA.Length

- 或 -

indexB 大于 strB.Length

- 或 -

indexAindexBlength 为负数。

- 或 -

strAstrBnull,并且 length 大于零。

ArgumentNullException

culturenull

The substrings to compare start in strA at indexA, and in strB at indexB. Both indexA and indexB are zero-based; that is, the first character in strA and strB is at position zero, not position one. The length of the first substring is equal to the length of strA minus indexA plus one. The length of the second substring is equal to the length of strB minus indexB plus one.

The number of characters to compare is the lesser of the lengths of the two substrings, and length. The indexA, indexB, and length parameters must be nonnegative.

The comparison uses the culture parameter to obtain culture-specific information such as casing rules and the alphabetic order of individual characters. For example, a culture could specify that certain combinations of characters be treated as a single character, or uppercase and lowercase characters be compared in a particular way, or that the sorting order of a character depends on the characters that precede or follow it.

The comparison is performed using word sort rules. For more information about word, string, and ordinal sorts, see T:System.Globalization.CompareOptions.

One or both comparands can be null. By definition, any string, including the empty string (""), compares greater than a null reference; and two null references compare equal to each other.

The comparison terminates when an inequality is discovered or both substrings have been compared. However, if the two strings compare equal to the end of one string, and the other string has characters remaining, then the string with remaining characters is considered greater. The return value is the result of the last comparison performed.

Unexpected results can occur when comparisons are affected by culture-specific casing rules. For example, in Turkish, the following example yields the wrong results because the file system in Turkish does not use linguistic casing rules for the letter "i" in "file".

static bool IsFileURI(String path)
{
    return (String.Compare(path, 0, "file:", 0, 5, true) == 0);
}

Compare the path name to "file" using an ordinal comparison. The correct code to do this is as follows:

static bool IsFileURI(String path)
{
    return (String.Compare(path, 0, "file:", 0, 5, StringComparison.OrdinalIgnoreCase) == 0);
}

调用函数说明:

Character sets include ignorable characters. The M:System.String.Compare(System.String,System.Int32,System.String,System.Int32,System.Int32,System.Boolean,System.Globalization.CultureInfo) method does not consider these characters when it performs a linguistic or culture-sensitive comparison. To recognize ignorable characters in your comparison, call the M:System.String.Compare(System.String,System.Int32,System.String,System.Int32,System.Int32,System.Globalization.CultureInfo,System.Globalization.CompareOptions) method and supply a value of F:System.Globalization.CompareOptions.Ordinal or F:System.Globalization.CompareOptions.OrdinalIgnoreCase for the options parameter.

The following example compares two substrings using different cultures and ignoring the case of the substrings. The choice of culture affects how the letter "I" is compared.

// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo)
using System;
using System.Globalization;

class Sample {
    public static void Main() {
//                 0123456
    String str1 = "MACHINE";
    String str2 = "machine";
    String str;
    int result;

    Console.WriteLine();
    Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
    Console.WriteLine("Ignore case, Turkish culture:");
    result = String.Compare(str1, 4, str2, 4, 2, true, new CultureInfo("tr-TR"));
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1);
    Console.Write("{0} ", str);
    Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2);

    Console.WriteLine();
    Console.WriteLine("Ignore case, invariant culture:");
    result = String.Compare(str1, 4, str2, 4, 2, true, CultureInfo.InvariantCulture);
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1);
    Console.Write("{0} ", str);
    Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2);
    }
}
/*
This example produces the following results:

str1 = 'MACHINE', str2 = 'machine'
Ignore case, Turkish culture:
Substring 'IN' in 'MACHINE' is less than substring 'in' in 'machine'.

Ignore case, invariant culture:
Substring 'IN' in 'MACHINE' is equal to substring 'in' in 'machine'.
*/

.NET Framework
自 1.1 起可用
返回页首
显示: