东儿童
爱拼才会赢

/// <summary>
        /// C#截取不超出指定长度的中英文混合字符串
        /// </summary>
        public string CutString(string s, int MaxLength)
        {
            char[] stringChar = s.ToCharArray();
            System.Text.StringBuilder sB = new System.Text.StringBuilder();
            System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
            int tmpLen = 0;
            for (Int32 i = 0; i < stringChar.Length; i++)
            {
                tmpLen += encoding.GetByteCount(stringChar, i, 1);
                if (tmpLen <= MaxLength)
                {
                    sB.Append(stringChar[i]);
                }
            }
            //byte[] b = System.Text.Encoding.GetEncoding("gb2312").GetBytes(s);

            //Response.Write("<br>"+b.Length.ToString()+"<br>"+stringChar[1].ToString()+"<br>"+s+"<br>"+sB.ToString());
            return sB.ToString();
        } 

posted on 2011-07-11 10:53  哎!无悔  阅读(417)  评论(0编辑  收藏  举报