byteLength
Description
Returns the number of bytes required to represent a UTF-8 encoded string.
Syntax
Like many functions in DataPrime, byteLength supports two notations, function and method. These interchangeable forms allow flexibility in how you structure expressions.
- Function notation
- Method notation
byteLength(value: string): number
(value: string).byteLength(): number
Arguments
| Name | Type | Required | Description |
|---|---|---|---|
value | string | true | The string to measure in UTF-8 bytes |
Example
Compare character count vs. byte count for a UTF-8 encoded string
Consider the following document:
{
"time_taken_str": "100μs"
}
Using length returns the number of characters:
create len from time_taken_str.length()
This produces:
{
"time_taken_str": "100μs",
"len": 5
}
However, this does not reflect the true byte size. The character μ requires two bytes in UTF-8.
Example query
- Function notation
- Method notation
create byte_len from byteLength(time_taken_str)
create byte_len from time_taken_str.byteLength()
Example output
{
"time_taken_str": "100μs",
"byte_len": 6
}
The four ASCII characters (1, 0, 0, s) each take one byte, and the μ symbol takes two bytes, for a total of six.