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

**URL:** <https://discourse.julialang.org/t/how-do-i-find-the-number-of-bytes-for-a-character/124144>\
**Category:** New to Julia\
**Tags:** strings, indexing, unicode\
**Created:** [December 24, 2024, 6:28am UTC](https://discourse.julialang.org/t/how-do-i-find-the-number-of-bytes-for-a-character/124144 "2024-12-24T06:28:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 24, 2024, 6:28am UTC](https://discourse.julialang.org/t/how-do-i-find-the-number-of-bytes-for-a-character/124144/1 "2024-12-24T06:28:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 24, 2024, 9:27am UTC](https://discourse.julialang.org/t/how-do-i-find-the-number-of-bytes-for-a-character/124144/2 "2024-12-24T09:27:36Z")

</div>

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

Thank you.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [December 24, 2024, 9:40am UTC](https://discourse.julialang.org/t/how-do-i-find-the-number-of-bytes-for-a-character/124144/3 "2024-12-24T09:40:07Z")

</div>

Alternatively, and more directly, you can use `ncodeunits`:

```julia-repl
julia> ncodeunits('a')
1

julia> ncodeunits('α')
2

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 24, 2024, 4:48pm UTC](https://discourse.julialang.org/t/how-do-i-find-the-number-of-bytes-for-a-character/124144/4 "2024-12-24T16:48:46Z")

</div>

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.

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

```

or

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

```
