JAVA算汉字真实长度

今天一个程序出了问题,排查了半天才发现,原来是因为原来的一个计算汉字长度的代码出了问题:

	public static int getStringRealLength(String str) throws Exception{
		String str1 = new String(str.getBytes("GB2312"),"iso-8859-1");
		return str1.length();
	}

汉字中有生僻字:“旸”,结果计算出长度为1。

后来将GB2312改成GBK就可以了。

你可能感兴趣的:(java)