string-Equals

        [Pure]
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static bool Equals(String a, String b, StringComparison comparisonType) {
            if (comparisonType < StringComparison.CurrentCulture || comparisonType > StringComparison.OrdinalIgnoreCase)
                throw new ArgumentException(Environment.GetResourceString("NotSupported_StringComparison"), "comparisonType");
            Contract.EndContractBlock();
 
            if ((Object)a==(Object)b) {
                return true;
            }
    
            if ((Object)a==null || (Object)b==null) {
                return false;
            }
 
            switch (comparisonType) {
                case StringComparison.CurrentCulture:
                    return (CultureInfo.CurrentCulture.CompareInfo.Compare(a, b, CompareOptions.None) == 0);
 
                case StringComparison.CurrentCultureIgnoreCase:
                    return (CultureInfo.CurrentCulture.CompareInfo.Compare(a, b, CompareOptions.IgnoreCase) == 0);
 
                case StringComparison.InvariantCulture:
                    return (CultureInfo.InvariantCulture.CompareInfo.Compare(a, b, CompareOptions.None) == 0);
 
                case StringComparison.InvariantCultureIgnoreCase:
                    return (CultureInfo.InvariantCulture.CompareInfo.Compare(a, b, CompareOptions.IgnoreCase) == 0);
 
                case StringComparison.Ordinal:
                    if (a.Length != b.Length)
                        return false;
 
                    return EqualsHelper(a, b);
 
                case StringComparison.OrdinalIgnoreCase:
                    if (a.Length != b.Length)
                        return false;
                    else {
                        // If both strings are ASCII strings, we can take the fast path.
                        if (a.IsAscii() && b.IsAscii()) {
                            return EqualsIgnoreCaseAsciiHelper(a, b);
                        }
                        // Take the slow path.
 
                        return (TextInfo.CompareOrdinalIgnoreCase(a, b) == 0);
                    }
 
                default:
                    throw new ArgumentException(Environment.GetResourceString("NotSupported_StringComparison"), "comparisonType");
            }
        }

compare方法

       [System.Security.SecuritySafeCritical]  // auto-generated
        public unsafe virtual int Compare(String string1, String string2, CompareOptions options){
 
            if (options == CompareOptions.OrdinalIgnoreCase)
            {
                return String.Compare(string1, string2, StringComparison.OrdinalIgnoreCase);
            }
 
            // Verify the options before we do any real comparison.
            if ((options & CompareOptions.Ordinal) != 0)
            {
                if (options != CompareOptions.Ordinal)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_CompareOptionOrdinal"), "options");
                        }
                return String.CompareOrdinal(string1, string2);
                    }
 
            if ((options & ValidCompareMaskOffFlags) != 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
            }
 
            //Our paradigm is that null sorts less than any other string and
            //that two nulls sort as equal.
            if (string1 == null) {
                if (string2 == null) {
                    return (0);     // Equal
                }
                return (-1);    // null < non-null
            }
            if (string2 == null) {
                return (1);     // non-null > null
            }
 
            return InternalCompareString(m_dataHandle, m_handleOrigin, m_sortName, string1, 0, string1.Length, string2, 0, string2.Length, GetNativeCompareFlags(options));
        }

其中null的比较,源码写的还是很完整的。

记录下常用的一种:

 bool rt = "AEba".Equals("AEBA", StringComparison.OrdinalIgnoreCase);// ture

 加一些比较记录方法。

4、避免不必要的调用ToUpper 或ToLower 方法

String是不变类,调用ToUpper或ToLower方法都会导致创建一个新的字符串。如果被频繁调用,将导致频繁创建字符串对象。这违背了前面讲到的“避免频繁创建对象”这一基本原则。

Compare 方法可以忽略大小写,一定场景可以避免使用ToUpper或ToLower

5、最快的字符串大小比较方法

将String对象的Length属性与0比较是最快的方法:if (str.Length == 0)

 其次是与String.Empty常量或空串比较:if (str == String.Empty)或if (str == "")

posted @ 2021-11-19 11:05  vba是最好的语言  阅读(43)  评论(0编辑  收藏  举报