计算字节的长度

//按字节截取字符串  
  static   public   string   getCountStr2(string   str,int   count)  
  {  
  byte   []   bwrite=Encoding.GetEncoding("GB2312").GetBytes(str.ToCharArray());  
  if(bwrite.Length>=count)  
  return   Encoding.Default.GetString(   bwrite,0,count-3);  
  else   return   Encoding.Default.GetString(bwrite);  
  }

一个 C# 字符串最终转化为多长的字节, 取决于使用的编码

      string s = "china 中华人民共和国";
      int l = 0;
      byte[] b1 = System.Text.Encoding.Default.GetBytes(s);
      l = b1.Length;//20
      byte[] b2 = System.Text.Encoding.UTF8.GetBytes(s);
      l = b2.Length;//27
      byte[] b3 = System.Text.Encoding.Unicode.GetBytes(s);
      l = b2.Length;//26
     varchar(255):Varchar类型,单位是字节的,一个中文字符两个字节,所以varchar(255),储存127个汉字和1个英语字符(除于2有余数)。
转换为字节数组  
  byte[]   by=System.Encoding.GetEncoding("GB2312").GetBytes(somestring);  
  int   len=by.Length;

c#判断输入的字符串的字节长度
using System.Text;

public partial class zijie : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string pp = getCountStr2("ffff",2);
        string mm = getCountStr2("敏敏",2);
    }
    /// <summary>
    /// 判断字节的长度
    /// </summary>
    /// <param name="str">要判断的字符串</param>
    /// <param name="count">要显示的长度</param>
    /// <returns>返回截取的字符串</returns>
    static public string getCountStr2(string str, int count)
    {
        byte[] bwrite = Encoding.GetEncoding("GB2312").GetBytes(str.ToCharArray());
        if (bwrite.Length >= count)
            return Encoding.Default.GetString(bwrite, 0, count - 0);
        else return Encoding.Default.GetString(bwrite);
    }
}

 


脚本判段输入的字节长度
<head runat="server">
    <title>无标题页</title>

    <script language="javascript" type="text/javascript">
function checkName(){
   
    String.prototype.Trim   =   function()   //给string增加个Trim()方法
{  
return   this.replace(/(^/s*)|(/s*$)/g,   "");  
}  
   
String.prototype.LTrim   =   function()    //给string增加个LTrim()方法,去左边的空格

{  
return   this.replace(/(^/s*)/g,   "");  
}  
   
String.prototype.RTrim   =   function() // 给string增加个RTrim()方法,去右边的空格

{  
return   this.replace(/(/s*$)/g,   "");  
}  


String.prototype.len = function()           // 给string增加个len ()方法,计算string的字节数
{
    return this.replace(/[^/x00-/xff]/g, "xx").length;
}

    var obj=document.getElementById("txtContent").value;   //取得textbox里的数据
    debugger;
    var count=obj.Trim().len();
    if(count>80)
    {
   
    }
    alert(count);  //去点空格后计算字节数
  
}
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
        <asp:TextBox ID="txtContent" Text="" runat="server"></asp:TextBox>
                <asp:Button ID="ddd" runat="server" Text="fff"  OnClientClick="return checkName()" OnClick="ddd_Click"/>
           </div>
    </form>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

18妹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值