/// <summary>
        /// 按指定(字节)长度截取字符串
        /// </summary>
        /// <param name="str">源字符串</param>
        /// <returns>string</returns>
        private string CutStringByte(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            if (System.Text.Encoding.Default.GetByteCount(str) < 48)
            {
                return str;
            }
            int i = 0;//字节数
            int j = 0;//实际截取长度
            foreach (char newChar in str)
            {
                if ((int)newChar > 127)
                {
                    //汉字
                    i += 2;
                }
                else
                {
                    i++;
                }
 
                if (i < 48)
                    j++;
                else
                    break;
            }
            str = str.Substring(0, j) + "...";
            return str;
        }
posted on 2016-03-30 17:39  zishen  阅读(5997)  评论(0编辑  收藏  举报