字符串的截取(2种截取)

        /// <summary>
        /// 截取字符串[汉字=1个字符]
        /// </summary>
        /// <param name="Strs">截取的内容</param>
        /// <param name="length">想要留下多少个字符</param>
        /// <param name="str">截取完,后面加点什么:比如 zxcv...</param>
        /// <returns>返回截取好的字符串</returns>
        public static string Intercep(string Strs,int length,string str)
        {
            if (Strs.Length > length)
            {
                Strs = Strs.Substring(0, length) + str;
            }
            return Strs;
        }

        /// <summary>
        /// 截取指定长度的字符串[汉字=2个字符]
        /// </summary>
        /// <param name="Strs">截取的内容</param>
        /// <param name="length">想要留下多少个字符</param>
        /// <param name="Str">截取完,后面加点什么:比如 zxcv...</param>
        /// <returns>返回截取好的字符串</returns>
        public static string Intercep2(string Strs, int length, string Str)
        {
            string tmp = Strs.Trim();
            int i = 0, j = 0;
            foreach (char chr in tmp)
            {
                if ((int)chr > 127)
                {
                    i += 2;
                }
                else
                {
                    i++;
                }
                if (i > length)
                {
                    tmp = tmp.Substring(0, j) + Str;
                    break;
                }
                j++;
            }
            return tmp;
        }

posted @ 2011-08-09 17:04  .net|C#|js|jquery|css|div|html  阅读(1507)  评论(0编辑  收藏  举报