public static int utf8Length(String string){
  CharacterIterator iter=new StringCharacterIterator(string);
  char ch=iter.first();
  int size=0;
  while (ch != CharacterIterator.DONE) {
    if ((ch >= 0xD800) && (ch < 0xDC00)) {
      char trail=iter.next();
      if ((trail > 0xDBFF) && (trail < 0xE000)) {
        size+=4;
      }
 else {
        size+=3;
        iter.previous();
      }
    }
 else     if (ch < 0x80) {
      size++;
    }
 else     if (ch < 0x800) {
      size+=2;
    }
 else {
      size+=3;
    }
    ch=iter.next();
  }
  return size;
}

public static int utf8Length ( String string ) { CharacterIterator iter = new StringCharacterIterator ( string ) ; char ch = iter . first ( ) ; int size = 0 ; while ( ch != CharacterIterator . DONE ) { if ( ( ch >= 0xD800 ) && ( ch < 0xDC00 ) ) { char trail = iter . next ( ) ; if ( ( trail > 0xDBFF ) && ( trail < 0xE000 ) ) { size += 4 ; } else { size += 3 ; iter . previous ( ) ; } } else if ( ch < 0x80 ) { size ++ ; } else if ( ch < 0x800 ) { size += 2 ; } else { size += 3 ; } ch = iter . next ( ) ; } return size ; }
for the given string , returns the number of utf - 8 bytes required to encode the string .
