日期:2014-05-20 浏览次数:21024 次
String s = "abcd";
byte[] bytes = s.getBytes();
System.out.println(s.length());
System.out.println(bytes.length);
s = "中国";
bytes = s.getBytes();
System.out.println(s.length());
System.out.println(bytes.length);
try {
String s = "中国";
System.out.println(s.length());
System.out.println(s.getBytes().length);
System.out.println(s.getBytes("GBK").length);
System.out.println(s.getBytes("UTF-8").length);
System.out.println(s.getBytes("ISO-8859-1").length);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
------解决方案--------------------
从第一个开始字符开始数,如果大于等于256就是2个字节,否则就是1个字节