2009/12/22

讓中文字長度等於2


由於客戶需要相容舊系統資料的關係
必須把中文字的長度算2
但在Unicode的架構下中文字的長度都是1

不過在C#下其實蠻簡單的
稍微轉個型就好
看程式碼

private void button1_Click(object sender, EventArgs e)
{
    //byte[] byteStr = Encoding.Default.GetBytes(textBox1.Text); //使用Default方法在非中文系統下可能會有問題
    byte[] byteStr = Encoding.GetEncoding("big5").GetBytes(textBox1.Text); //把string轉為byte 
    label1.Text = byteStr.Length.ToString(); //取byte的長度, 中文字就會算2碼了
}

 


[Code下載]

 

 

by sam319

0 意見: