C#中字符串的比较操作===
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C#中字符串的比较操作
字符串比较是比较常用的操作,一般出于以下两个原因比较字符串:
∙判断相等
∙字符串排序
查询API判断字符串相等或排序时,由以下方法:
public override bool Equals(object obj);
public bool Equals(string value);
public static bool Equals(string a, string b);
public bool Equals(string value, StringComparison comparisonType);
public static bool Equals(string a, string b, StringComparison comparisonType);
public static int Compare(string strA, string strB);
public static int Compare(string strA, string strB, bool ignoreCase);
public static int Compare(string strA, string strB, StringComparison comparisonType);
public static int Compare(string strA, string strB, bool ignoreCase, CultureInfo culture);
public static int Compare(string strA, string strB, CultureInfo culture, CompareOptions options);
public static int Compare(string strA, int indexA, string strB, int indexB, int length);
public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase);
public static int Compare(string strA, int indexA, string strB, int indexB, int length, StringComparison comparisonType);
public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase, CultureInfo culture);
public static int Compare(string strA, int indexA, string strB, int indexB, int length, CultureInfo culture, CompareOptions options);
public static int CompareOrdinal(string strA, string strB);
public static int CompareOrdinal(string strA, int indexA, string strB, int indexB, int length);
public int CompareTo(object value);
public int CompareTo(string strB);
发现上述的方法中大多都有StringComparison类型的枚举,查询msdn后得到:
现简单写一段代码,测试Compare(string strA, string strB, StringComparison comparisonType)方法。分别用到StringComparison.CurrentCulture 和StringComparison.Ordinal。代码如下:
static void Main(string[] args)
{
string strA = "asdfadsfasdfew我ò啊?地?方?的?asd";
string strB = "adsfeaqfaead啊?多à发¢安2德?森-efadsfa";
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
pare(strA, strB, StringComparison.CurrentCulture);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
for (int i = 0; i < 1000000; i++)
{
pare(strA, strB,StringComparison.Ordinal);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Reset();
for (int i = 0; i < 1000000; i++)
{
pareOrdinal(strA, strB);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}
执行结果如下:
测试结果非常明显,StringComparison.Currentculture显式传递了当前语言文化,而传递了String.Ordinal则会忽略指定的语言文化,这是执行字符串最快的一种方式。
使用.NET Reflector查看源代码:
public static int Compare(string strA, string strB, StringComparison comparisonType)
{
if ((comparisonType < StringComparison.CurrentCulture) || (comparisonType > StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException(Environment.GetResourceString("NotSupported_StringComparison"), "comparisonType");
}
if (strA == strB)
{
return 0;
}
if (strA == null)
{
return -1;
}
if (strB == null)
{
return 1;
}
switch (comparisonType)
{
case StringComparison.CurrentCulture:
return pare(strA, strB, CompareOptions.None);
case StringComparison.CurrentCultureIgnoreCase:
return pare(strA, strB, CompareOptions.IgnoreCase);
case StringComparison.InvariantCulture:
return pare(strA, strB, CompareOptions.None);
case StringComparison.InvariantCultureIgnoreCase:
return pare(strA, strB, CompareOptions.IgnoreCase);
case StringComparison.Ordinal:
return CompareOrdinalHelper(strA, strB);
case StringComparison.OrdinalIgnoreCase:
if (!strA.IsAscii() || !strB.IsAscii())