You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
have access to post topics
communicate privately with other members (PM)
not see advertisements between posts
have the possibility to earn one of our surprises if you are an active member
access many other special features that will be introduced later.
I want to find out number of bytes in a string
how can I do that?
there is a getbyte function in String class, but it returns a Byte array
I want to get the number of bytes occupied by a string.
can you help me?
This is a more complicated question then it appears. What are you using the value for? The reason is that a Java String object is stored internally as unicode.
You can get the number of bytes by doing the following:
String str = "My name";
int bytes = str.getBytes().length;
But this number depends on the character encoding of my current platform.
There is also the length() method on the String class that will tell you the number of characters in the string.