C#截取字符串长度方法

方法一、

        /// <summary>
        /// 截取指定长度的字符串
        /// </summary>
        /// <param name="str">要截取的字符串</param>
        /// <param name="len">要截取的长度</param>
        /// <param name="flag">截取后是否加省略号(true加,false不加)</param>
        /// <returns></returns>
        public static string CutString(string str, int len, bool flag)
        {
            string _outString = "";
            int _len = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (Char.ConvertToUtf32(str, i) >= Convert.ToInt32("4e00", 16) && Char.ConvertToUtf32(str, i) <= Convert.ToInt32("9fff", 16))
                {
                    _len += 2;
                    if (_len > len)//截取的长度若是最后一个占两个字节,则不截取
                    {
                        break;
                    }
                }
                else
                {
                    _len++;
                }


                try
                {
                    _outString += str.Substring(i, 1);
                }
                catch
                {
                    break;
                }
                if (_len >= len)
                {
                    break;
                }
            }
            if (str != _outString && flag == true)//判断是否添加省略号
            {
                _outString += "...";
            }
            return _outString;
        }

 


方法二、

        /// <summary>
        /// 截取指定长度的字符串
        /// </summary>
        /// <param name="str">要截取的字符串</param>
        /// <param name="len">要截取的长度</param>
        /// <param name="flag">截取后是否加省略号(true加,false不加)</param>
        /// <returns></returns>
        public static string CutString(string str, int len, bool flag)
        {
            System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
            byte[] bts = ascii.GetBytes(str);
            int _len = 0;
            string _outString = "";
            for (int i = 0; i < bts.Length; i++)
            {
                if ((int)bts[i] == 63)//两个字符
                {
                    len += 2;
                    if (_len > len)//截取的长度若是最后一个占两个字节,则不截取
                    {
                        break;
                    }
                }
                else
                {
                    len += 1;
                }

                try
                {
                    _outString += str.Substring(i, 1);
                }
                catch
                {
                    break;
                }

                if (_len >= len)
                {
                    break;
                }
            }
            if (str != _outString && flag == true)//判断是否添加省略号
            {
                _outString += "...";
            }
            return _outString;
        }

 

方法三、

        /// <summary>
        /// 截取指定长度的字符串
        /// </summary>
        /// <param name="str">要截取的字符串</param>
        /// <param name="len">要截取的长度</param>
        /// <param name="flag">截取后是否加省略号(true加,false不加)</param>
        /// <returns></returns>
        public static string CutString(string str, int len, bool flag)
        {
            if (str == null || str.Length == 0 || len <= 0)
            {
                return string.Empty;
            }

            int l = str.Length;

            #region 计算长度
            int clen = 0;
            while (clen < len && clen < l)
            {
                //每遇到一个中文,则将目标长度减一。
                if ((int)str[clen] > 128)
                {
                    len--;
                }
                clen++;
            }
            #endregion

            if (clen < l)
            {
                return flag ? str.Substring(0, clen) + "..." : str.Substring(0, clen);
            }
            else
            {
                return str;
            }
        }

 

 

posted @ 2012-09-16 01:02  ゞ修ζ止符℡_R²º¹³  阅读(2609)  评论(2编辑  收藏  举报