-3

Do we any lib or method which will tell us the size of string value in bytes , am checking for size not length .

can anyone help me on this

.getBytes().length gives the length of the string not size , for example

String s = "Apple"

s.getBytes().length returns 5 , i think this length not size

4

1 Answer 1

0

There is a method called getBytes() 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.

    final String string = "Hello World";

    final byte[] utf8Bytes = string.getBytes("UTF-8");
    System.out.println(utf8Bytes.length); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.