[jdev] Re: Get the length of the utf-8 sequence in Java

Cedric Vivier cedricv at neonux.com
Thu Sep 9 04:19:48 CDT 2004

I do not believe Java has a standard method for this in the standard 
library, but you could implement yours :

public int byte_length(String s) {
     int numchars = s.length();
     int numbytes = 0;

     for (int i = 0 ; i < numchars ; i++) {
       int c = s.charAt(i);
       if ((c >= 0x0001) && (c <= 0x007F)) numbytes++;
       else if (c > 0x07FF) numbytes += 3;
       else numbytes += 2;

     return numbytes;

I have no idea if it would be faster than your current method though, 
but it should be more memory-efficient at least.


More information about the JDev mailing list