首页 > 其他 > 详细

不区分中英文截断字符串

时间:2015-05-27 17:26:00      阅读:326      评论:0      收藏:0      [点我收藏+]
public static string GetString(this HtmlHelper htmlHelper, string s, int l, string endStr)
        {
            string temp = s.Substring(0, (s.Length < l + 1) ? s.Length : l + 1);
            byte[] encodedBytes = Encoding.ASCII.GetBytes(temp);


            string outputStr = string.Empty;
            int count = 0;


            for (int i = 0; i < temp.Length; i++)
            {
                if (encodedBytes[i] == 63)
                {
                    count += 2;
                }
                else
                {
                    count += 1;
                }


                if (count <= l - endStr.Length)
                {
                    outputStr += temp.Substring(i, 1);
                }
                else if (count > l)
                {
                    break;
                }
            }


            if (count <= l)
            {
                outputStr = temp;
                endStr = string.Empty;
            }


            outputStr += endStr;


            return outputStr;
        }

不区分中英文截断字符串

原文:http://blog.csdn.net/duwujunjun/article/details/46048103

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!