当前位置:网站首页>String.length()和String.getBytes().length的区别
String.length()和String.getBytes().length的区别
2022-04-21 20:28:00 【XuDream】
String.length()和String.getBytes().length的区别
String.length() 是表示字符串所需的UTF-8代码单元的数量。
String.getBytes().length 是在默认编码中表示字符串所需的字节数。
举例说明:
public static void main(String[] args) {
String utf="张";
System.out.println("字符串长度="+utf.length()+",字符串字节"+utf.getBytes(Charset.forName("UTF-8")).length);
System.out.println("字符串长度="+utf.length()+",字符串字节"+utf.getBytes(Charset.forName("GBK")).length);
}
输出结果:
字符串长度=1,字符串字节3
字符串长度=1,字符串字节2
总结:
UTF编码方式中一个汉字三个字节、GBK编码方式中一个汉字两个字节,UTF/GBK其余都是一个字节.
版权声明
本文为[XuDream]所创,转载请带上原文链接,感谢
https://blog.csdn.net/XuDream/article/details/124277071
边栏推荐
猜你喜欢

vmware-vmx.exe无法结束进程

< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (I)

How does the Mui tab realize circular rotation

Android Development Internship interview questions, Android development interview basis

实战 | 电商业务性能测试(二): Jmeter 参数化功能实现注册登录的数据驱动

ROS knowledge: how to realize camera access

TCP example of grpc implemented by golang

In the morning, I met Tencent and took out 38K, which let me see the basic ceiling
![[gradle] problem analysis + download and installation + environment configuration + installation verification](/img/a6/d6ea65127fbbed259f5f22faf23839.png)
[gradle] problem analysis + download and installation + environment configuration + installation verification

C语言求完全平方数
随机推荐
《常识题题库系统》,公务员必备,博学广识之士必备。从程序员变成诗人
Cuda02 - memory access optimization and unified memory
“国潮”积木,潮玩的下一个答案?
Common sense question bank system is a must for civil servants and knowledgeable people. From programmer to poet
Liu Zhigang, former COO of Jinshan cloud, joined Baihai technology as president
<2021SC@SDUSC>山东大学软件工程应用与实践JPress小组课题介绍
Redis的两种基准性能测试方式
LeetCode_70 爬楼梯
C语言求完全平方数
How to let the back door of the network card kill a system and let you know how powerful the network card is
JUC queue interface and its implementation class
外包干了五年,差不多是个废人了
glew, glee与 gl glu glut glx glext的区别和关系
< 2021SC@SDUSC > Introduction to the jpress group on software engineering application and practice of Shandong University
Status code encapsulation -- reprint
shell:变量
双链表插入、删除操作单步解析(十四)
实战 | JMeter 典型电商场景(下单/支付)的性能压测
Im instant messaging development technology: 1-10 million high concurrency architecture evolution
长安深蓝C385产品信息曝光 瞄准20万级别,头号目标Model 3!