1. 程式人生 > >C#中字串的比較操作

C#中字串的比較操作

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 CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.None); case StringComparison.CurrentCultureIgnoreCase: return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions
.IgnoreCase); case StringComparison.InvariantCulture: return CultureInfo.InvariantCulture.CompareInfo.Compare(strA, strB, CompareOptions.None); case StringComparison.InvariantCultureIgnoreCase: return CultureInfo.InvariantCulture.CompareInfo.Compare(strA, strB, CompareOptions.IgnoreCase); case StringComparison.Ordinal: return CompareOrdinalHelper(strA, strB); case StringComparison.OrdinalIgnoreCase: if (!strA.IsAscii() || !strB.IsAscii()) { return TextInfo.CompareOrdinalIgnoreCase(strA, strB); } return CompareOrdinalIgnoreCaseHelper(strA, strB); } throw new NotSupportedException(Environment.GetResourceString("NotSupported_StringComparison")); }