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就可以了。因为字符可以正确算出长度。
原帖地址:https://blog.csdn.net/soundfuture/article/details/11601653
汉字所占byte问题请参考我的博文:https://blog.csdn.net/woshixuye/article/details/7907537
最后更新:2017-04-03 12:54:31
上一篇:
unity 脚本中的一些attribute
下一篇:
Linux 共享内存 详解
ASP.NET Core中的依赖注入(4): 构造函数的选择与服务生命周期管理
网络子系统7_l2、l3接口
magento -- 为Magento提速再做一点小努力之移除不需要的JS
Oracle Resource Manager和调度任务
网络安全的未来:主动弹性
值得推荐的 PHP 连接 mssql的文章
阿里云服务器最便宜的多少钱一年?
4月24日云栖精选夜读:AI不可怕,就怕AI会画画——这里有一种你还不知道的‘图’灵测试…
Only the original thread that created a view hierarchy can touch its views——Handler的使用
【对标TensorFlow】阿里公开内部超大规模分布式机器学习平台