zoukankan      html  css  js  c++  java
  • C#中英文字符长度截取

    /// <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();
            } 

  • 相关阅读:
    window.location无法跳转页面的问题
    html doctype作用
    ThinkPhp学习09
    ThinkPhp学习08
    ThinkPhp学习07
    ThinkPhp学习06
    ThinkPhp学习05
    Thinkphp学习04
    ThinkPhp学习03
    ThinkPhp学习02
  • 原文地址:https://www.cnblogs.com/liuguanghuiyes/p/2102809.html
Copyright © 2011-2022 走看看