Reputation: 710
public class ClassToTestSnippets {
private static ClassToTestSnippets ctts;
public static void main(String[] args) {
ctts = new ClassToTestSnippets();
ctts.testThisMethod();
}
public void testThisMethod() {
System.out.println("\u2014".length()); //answer is 1
}
}
Above code prints 1. But \u2014
is E2 80 94
i.e. 3 bytes. How do I know how many bytes does a string contains?
Upvotes: 1
Views: 6935
Reputation: 34638
Internally - it contains (number of chars) * 2 bytes, as each char
in Java takes up two bytes (a normal character in Java is 16 bits unicode). The actual bytes are 0x20 and 0x14.
However, the length function returns the number of characters, not the number of bytes.
Upvotes: 4
Reputation: 73568
Depends. What encoding do you want to use?
System.out.println("äö".getBytes("UTF-8").length);
Prints 4, but if I change UTF-8
to ISO-8859-1
(for example), it'll print 2. Other encodings may print other values (try UTF-32
).
Upvotes: 9