String.Compare 方法 (String, Int32, String, Int32, Int32)
对两个指定的 String 对象的子字符串进行比较,并返回一个指示二者在排序顺序中的相对位置的整数。
参数
- strA
-
类型:System.String
要在比较中使用的第一个字符串。
- indexA
-
类型:System.Int32
strA 中子字符串的位置。
- strB
-
类型:System.String
要在比较中使用的第二个字符串。
- indexB
-
类型:System.Int32
strB 中子字符串的位置。
- length
-
类型:System.Int32
要比较的子字符串中字符的最大数量。
返回值
类型:System.Int32 一个 32 位有符号整数,指示两个比较数之间的词法关系。 值 Condition
小于零 strA 中的子字符串小于 strB 中的子字符串。
零 子字符串相等,或者 length 为零。
大于零 strA 中的子字符串大于 strB 中的子字符串。
// Sample for String.Compare(String, Int32, String, Int32, Int32)
using System;
class Sample {
public static void Main() {
// 0123456
String str1 = "machine";
String str2 = "device";
String str;
int result;
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
result = String.Compare(str1, 2, str2, 0, 2);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
}
}
/*
This example produces the following results:
str1 = 'machine', str2 = 'device'
Substring 'ch' in 'machine' is less than substring 'de' in 'device'.
*/
http://msdn.microsoft.com/zh-cn/library/x7tax739.aspx