是用1这个方法效率高还是用我自己的方法效率高?假设这个这个字符串有100万个字节!!
1) string.SubString(int,int)
2)我自己的方法
public static string GetSubString(string Str, int Num)
        {
            string titlemore = lk.config.titlemore;
            if (Str == null || Str == "")
            {
                return "";
            }
            string outstr = string.Empty;
            int n = 0;
            foreach (char ch in Str)
            {
                n += System.Text.Encoding.Default.GetByteCount(ch.ToString());
                if (n > Num)
                {
                    break;
                }
                else
                {
                    outstr += ch;
                }
            }
            if (titlemore == "1")
            {
                if (Str.Length > Num)
                {
                    outstr += "...";
                }
            }
            return outstr;
        }

解决方案 »

  1.   

    如果你写的方法能比微软的SubString方法效率高,那么你就可以参加微软的团队了。
      

  2.   


    //String.SubString的反编译的结果(代码片断,lz自己判断哪个效率高吧)private unsafe string InternalSubString(int startIndex, int length, bool fAlwaysCopy)
    {
        if (((startIndex == 0) && (length == this.Length)) && !fAlwaysCopy)
        {
            return this;
        }
        string str = FastAllocateString(length);
        fixed (char* chRef = &str.m_firstChar)
        {
            fixed (char* chRef2 = &this.m_firstChar)
            {
                wstrcpy(chRef, chRef2 + startIndex, length);
            }
        }
        return str;
    }