bytesCount method

int bytesCount()

Calculates the byte count of a string, treating non-ASCII characters as 2 bytes.

This is often used for validation where systems have byte-based length limits (e.g., some database varchar fields).

Example:

'你好'.bytesCount(); // 4
'hello你好'.bytesCount( ) ;  // 9

Implementation

int bytesCount() {
  return this.replaceAll(RegExp(r"[^\u0000-\u00ff]"), "aa").length;
}