Results 1 to 3 of 3
  1. #1
    gabriel is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default how to know the number of bytes

    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?

  2. #2
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    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.

  3. #3
    gabriel is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

Similar Threads

  1. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 03:46 PM
  2. The highest number
    By I-Shine in forum Java Applets
    Replies: 3
    Last Post: 02-13-2008, 05:05 AM
  3. Reading bytes from InputStream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-25-2007, 07:51 PM
  4. Replies: 3
    Last Post: 11-18-2007, 07:41 PM
  5. how to know the number of the line
    By simon in forum New To Java
    Replies: 3
    Last Post: 08-01-2007, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •