How do I find the number of bytes for a character?

Hello,
Please take a look at the following text:

Consider the following:
julia> grk = “β+θ”
“β+θ”
julia> length(grk)
3
julia> sizeof(grk)
5
Since β and θ occupy two bytes each, the valid indices into grk are 1 (β), 3 (+) and 4 (θ).

How can I find the number of bytes a character occupies?

Thank you.

Hello,
I found it. I can do this with the help of eachindex(str) function.

Thank you.

1 Like

Alternatively, and more directly, you can use ncodeunits:

julia> ncodeunits('a')
1

julia> ncodeunits('α')
2
8 Likes

If what you are trying to do is to iterate over the valid indices of a string, you should normally use either eachindex or nextind. e.g.

for i in eachindex(str)
   # do something with str[i]
end

or

i, e = firstindex(str), lastindex(str)
while i <= e
    # do something with str[i]
    i = nextind(str, i)
end
2 Likes