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();
}
}
{
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();
}
}
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
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());
}
}
我觉得楼主在做插入数据库之前的长度判断。