sql >> Base de Datos >  >> RDS >> Oracle

Java:cuente exactamente 60 caracteres de una cadena con una combinación de caracteres UTF-8 y no UTF-8

Según tengo entendido, desea limitar la String longitud de manera que el UTF-8 codificado representación no supera los 60 bytes. Puedes hacerlo de esta manera:

String s=…;
CharsetEncoder enc=StandardCharsets.UTF_8.newEncoder();
ByteBuffer bb=ByteBuffer.allocate(60);// note the limit
CharBuffer cb = CharBuffer.wrap(s);
CoderResult r = enc.encode(cb, bb, true);
if(r.isOverflow()) {
    System.out.println(s+" is too long for "
                      +bb.capacity()+" "+enc.charset()+" bytes");
    s=cb.flip().toString();
    System.out.println("truncated to "+s);
}