如:
每个汉字返回2,每个英文字符返回1

解决方案 »

  1.   

    TextBox1.Text.Trim().Length读出的中文每个汉字是 1 嘛?!
      

  2.   


     
    在字符串转换到字节数组的过程中,Encoding.Default 会将每个单字节字符,如半角英文,转换成 1 个字节,而把每个双字节字符,如汉字,转换成 2 个字节。而 Encoding.Unicode 则会将它们都转换成两个字节。我们可以通过下列简单的了解一下转换的方法,以及使用 Encoding.Default 和 Encodeing.Unicode 的区别:private void TestStringBytes() {
        string s = "C#语言";
        byte[] b1 = System.Text.Encoding.Default.GetBytes(s);
        byte[] b2 = System.Text.Encoding.Unicode.GetBytes(s);
        string t1 = "", t2 = "";
        foreach (byte b in b1) {
            t1 += b.ToString("") + " ";
        }
        foreach (byte b in b2) {
            t2 += b.ToString("") + " ";
        }
        this.textBox1.Text = "";
        this.textBox1.AppendText("b1.Length = " + b1.Length + "\n");
        this.textBox1.AppendText(t1 + "\n");
        this.textBox1.AppendText("b2.Length = " + b2.Length + "\n");
        this.textBox1.AppendText(t2 + "\n");
    }
    运行结果如下。b1.Length = 6
    67 35 211 239 209 212 
    b2.Length = 8
    67 0 35 0 237 139 0 138
      

  3.   

    Private Function GetLength(ByVal str As String,Byval length as string) As string
         Dim encodingData() As Byte        encodingData = System.Text.Encoding.Default.GetBytes(str)
           If length < encodingData.Length Then
                Return System.Text.Encoding.Default.GetString(encodingData, 0, length )
            Else
                Return str 
            End If End Function
      

  4.   

    http://blog.csdn.net/rickjelly2004/archive/2004/11/11/getLengthsubstringB.aspx