c#截取字符串长度

 /// <summary>
        /// 截短字串的函数
        /// </summary>
        /// <param name="mText">要加工的字串</param>
        /// <param name="byteCount">长度</param>
        /// <returns>被加工过的字串</returns>
        public static string CutStr_New(string mText, int byteCount)
        {
            if (byteCount < 1) return mText;

            if (System.Text.Encoding.Default.GetByteCount(mText) <= byteCount)
            {
                return mText;
            }
            else
            {
                byte[] txtBytes = System.Text.Encoding.Default.GetBytes(mText);
                byte[] newBytes = new byte[byteCount - 4];

                for (int i = 0; i < byteCount - 4; i++)
                    newBytes[i] = txtBytes[i];

                return System.Text.Encoding.Default.GetString(newBytes) + "...";
            }
        }

猜你喜欢

转载自www.cnblogs.com/zengxh/p/12390758.html
今日推荐