package wuziqi;import java.io.UnsupportedEncodingException;public class temp
{
String str="你好啊";
public temp()
{

try {
byte[] a = str.getBytes("UTF-8");
System.out.println(a.length);
} catch (UnsupportedEncodingException e) {
}

} public static void main(String args[])
{
temp obj=new temp();
}
}

解决方案 »

  1.   

    它的Unicode长度就是3呀每个Unicode占用2个字节,字节数是6
      

  2.   

    Unicode中文长度是不会变的,英文字母从1字节变为2字节
      

  3.   

    来看一下文档:
    lengthpublic int length()    Returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string.    Specified by:
            length in interface CharSequence    Returns:
            the length of the sequence of characters represented by this object.注意这么一句话:
    The length is equal to the number of 16-bit Unicode characters in the string.
    一个汉字只占一个 16-bit Unicode
      

  4.   

    看看这个例子:
    import java.io.*;
    public class StringTest 
    {
        public static void main(String[] args) 
        {
            String aString = "这是一个测试串,This is a test string.";
            String anotherString = null;
            try {
               anotherString = new String(aString.getBytes("GBK"), "ISO8859_1");
            }
            catch (UnsupportedEncodingException ex) {}
            System.out.println(aString.length() + "," + anotherString.length());
        }

      

  5.   

    楼主应该是要UTF-8的长度,是1-6变长编码,我上面的编码可行。
    我觉得楼主在做插入数据库之前的长度判断。