Difference between String.length() and String.getBytes().length

前端 未结 4 764
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 19:30

I am beginner and self-learning in Java programming. So, I want to know about difference between String.length() and String.getBytes().length in Ja

相关标签:
4条回答
  • 2020-12-08 20:00

    String.length()

    String.length() is the number of 16-bit UTF-16 code units needed to represent the string. That is, it is the number of char values that are used to represent the string and thus also equal to toCharArray().length. For most characters used in western languages this is typically the same as the number of unicode characters (code points) in the string, but the number of code point will be less than the number of code units if any UTF-16 surrogate pairs are used. Such pairs are needed only to encode characters outside the BMP and are rarely used in most writing (emoji are a common exception).

    String.getBytes().length

    String.getBytes().length on the other hand is the number of bytes needed to represent your string in the platform's default encoding. For example, if the default encoding was UTF-16 (rare), it would be exactly 2x the value returned by String.length() (since each 16-bit code unit takes 2 bytes to represent). More commonly, your platform encoding will be a multi-byte encoding like UTF-8.

    This means the relationship between those two lengths are more complex. For ASCII strings, the two calls will almost always produce the same result (outside of unusual default encodings that don't encode the ASCII subset in 1 byte). Outside of ASCII strings, String.getBytes().length is likely to be longer, as it counts bytes needed to represent the string, while length() counts 2-byte code units.

    Which is more suitable?

    Usually you'll use String.length() in concert with other string methods that take offsets into the string. E.g., to get the last character, you'd use str.charAt(str.length()-1). You'd only use the getBytes().length if for some reason you were dealing with the array-of-bytes encoding returned by getBytes.

    0 讨论(0)
  • 2020-12-08 20:14

    The length() method returns the length of the string in characters.

    Characters may take more than a single byte. The expression String.getBytes().getLength() returns the length of the string in bytes, using the platform's default character set.

    0 讨论(0)
  • 2020-12-08 20:15

    In short, String.length() returns the number of characters in the string while String.getBytes().length returns the number of bytes to represent the characters in the string with specified encoding.

    In many cases, String.length() will have the same value as String.getBytes().length. But in cases like encoding UTF-8 and the character has value over 127, String.length() will not be the same as String.getBytes().length. Here is an example which explains how characters in string is converted to bytes when calling String.getBytes(). This should give you a sense of the difference between String.length() and String.getBytes().length.

    0 讨论(0)
  • 2020-12-08 20:20

    The String.length() method returns the quantity of symbols in string. While String.getBytes().length() returns number of bytes used to store those symbols. Usually chars are stored in UTF-16 encoding. So it takes 2 bytes to store one char. Check this SO answer out.

    I hope that it will help :)

    0 讨论(0)
提交回复
热议问题