41

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

What is more suitable to check the length of the string?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Key
  • 425
  • 1
  • 4
  • 9
  • What did your experiments suggest? – Andrew Thompson Apr 29 '13 at 04:04
  • Have you tried using it on a unicode String? – Patashu Apr 29 '13 at 04:04
  • 3
    `string.length` will give the number of characters in a string, where as `string.getBytes().length` will give number of bytes used by the string to store the value. In normal circumstances each character can take 2 bytes each to store a unicode value – Arun P Johny Apr 29 '13 at 04:05
  • 1
    @ArunPJohny: No, `string.length` will return the number of "Unicode code units" in the string. For ASCII text, it will count characters. For arbitrary Unicode text, it's... complicated. See BeeOnRope's answer for the ugly details. – sleske Feb 15 '16 at 14:29

4 Answers4

49

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.

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
9

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

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

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
2

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.

Taner
  • 4,511
  • 3
  • 18
  • 14
FreeNickname
  • 7,398
  • 2
  • 30
  • 60
2

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.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
PixelsTech
  • 3,229
  • 1
  • 33
  • 33