byteLength - Get the number of bytes in a UTF-8 encoded String
The byteLength
function will return the count of the bytes in a UTF-8 encoded string.
Syntax
Arguments
Name | Type | Required | Description |
---|---|---|---|
string | string | true | The string whose bytes will be counted |
Example - Getting true byte length of UTF-8 String
Consider the following document:
If we wish to assess the number of bytes in this string, a naive way to do it is to calculate this using length
:
This will create a field len
with value 5
. This is the number of characters, but it is not the true length of the string in bytes. That is because in UTF-8, the μ
symbol requires 2 bytes to store in UTF-8.
Instead, we can use byteLength
to calculate the number of bytes it needs to store this string, encoded in UTF-8:
This will create a field byte_len
which has the value 6
, representing the four characters that require a single byte, 1
,0
,0
and s
, along with the single character that requires two bytes, μ
.
Theme
Light