getUtf8ByteLength static method

int getUtf8ByteLength(
  1. String str
)

Get UTF-8 byte length of a string without allocating memory

Returns the number of bytes required to encode the string in UTF-8, without actually allocating the native string. Useful for pre-calculating buffer sizes.

Example:

final byteLength = StringUtils.getUtf8ByteLength("Hello世界");
// byteLength = 11 (5 ASCII + 6 for two Chinese characters)

Implementation

static int getUtf8ByteLength(String str) {
  return utf8.encode(str).length;
}