【问题标题】:How to find size (not length ) of a string value in java? [duplicate]如何在java中查找字符串值的大小(不是长度)? [复制]
【发布时间】:2020-03-17 05:05:41
【问题描述】:

我们是否有任何库或方法可以告诉我们字符串值的大小(以字节为单位),正在检查大小而不是长度。

谁能帮我解决这个问题

.getBytes().length 给出字符串的长度而不是 size ,例如

String s = "Apple"

s.getBytes().length 返回 5,我认为这个长度不是大小

【问题讨论】:

标签: java string


【解决方案1】:

有一个方法叫做 getBytes() string.getBytes().length 将给出字符串用于存储值的字节数。在正常情况下,每个字符可以占用 2 个字节来存储一个 unicode 值。

    final String string = "Hello World";

    final byte[] utf8Bytes = string.getBytes("UTF-8");
    System.out.println(utf8Bytes.length); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多