# How to iterate over unicode characters with multiple codepoints

**URL:** <https://discourse.julialang.org/t/how-to-iterate-over-unicode-characters-with-multiple-codepoints/47828>\
**Category:** New to Julia\
**Created:** [October 6, 2020, 3:13am UTC](https://discourse.julialang.org/t/how-to-iterate-over-unicode-characters-with-multiple-codepoints/47828 "2020-10-06T03:13:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![systems](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@systems](https://discourse.julialang.org/u/systems)\
**Post date:** [October 6, 2020, 3:13am UTC](https://discourse.julialang.org/t/how-to-iterate-over-unicode-characters-with-multiple-codepoints/47828/1 "2020-10-06T03:13:45Z")

</div>

Hi this code

```julia
str = "Héllo World"
for i in str 
    println(i)
end

```

produce this output

```julia
H
e
́
l
l
o

W
o
r
l
d

```

How to make it produce

```julia
H
é
l
l
o

W
o
r
l
d 

```

(On a side note, if you run it in vs code repl the line with only ́ from the first output looks empty)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 6, 2020, 4:34am UTC](https://discourse.julialang.org/t/how-to-iterate-over-unicode-characters-with-multiple-codepoints/47828/2 "2020-10-06T04:34:57Z")

</div>

looks like they are truly two characters:

```julia
julia> ary = [str[2:3]...]
2-element Array{Char,1}:
 'e': ASCII/Unicode U+0065 (category Ll: Letter, lowercase)
 '́': Unicode U+0301 (category Mn: Mark, nonspacing)

julia> join(ary)
"é"

julia> ary[1] = 'a'; join(ary)
"á"

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 6, 2020, 5:54am UTC](https://discourse.julialang.org/t/how-to-iterate-over-unicode-characters-with-multiple-codepoints/47828/3 "2020-10-06T05:54:18Z")

</div>

Unicode indexing is best done via `eachindex`, which returns the valid byte indices corresponding to unicode codepoints.

That said, @jling is correct - `´` in unicode is a valid “character”, though a combining mark.

[The manual](https://docs.julialang.org/en/v1/manual/strings/) has a lot of very useful information about this.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [October 6, 2020, 1:08pm UTC](https://discourse.julialang.org/t/how-to-iterate-over-unicode-characters-with-multiple-codepoints/47828/4 "2020-10-06T13:08:33Z")

</div>

Note that this works as expected

```julia
julia> str = "Héllo World"
"Héllo World"

julia> str1 = "Héllo World"
"Héllo World"

julia> split(str, "")
12-element Array{SubString{String},1}:
 "H"
 "e"
 "́"
 "l"
 "l"
 "o"
 " "
 "W"
 "o"
 "r"
 "l"
 "d"

julia> split(str1, "")
11-element Array{SubString{String},1}:
 "H"
 "é"
 "l"
 "l"
 "o"
 " "
 "W"
 "o"
 "r"
 "l"
 "d"

```

it depends on whether you use U+0065 and U+0301 or U+00E9.  
However, in cases that require combinations the question remains open.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [October 6, 2020, 1:28pm UTC](https://discourse.julialang.org/t/how-to-iterate-over-unicode-characters-with-multiple-codepoints/47828/5 "2020-10-06T13:28:06Z")

</div>

You can use `Unicode.graphemes` to iterate over graphemes (“user-perceived characters” in unicode), regardless of how they are encoded in code points:

```julia
julia> using Unicode

julia> graphemes("Héllo World")
length-11 GraphemeIterator{String} for "Héllo World"

julia> graphemes("Héllo World") |> collect
11-element Array{SubString{String},1}:
 "H"
 "é"
 "l"
 "l"
 "o"
 " "
 "W"
 "o"
 "r"
 "l"
 "d"

```

Note that the second element of this array is a string of 2 code points (“2 characters” in the terminology of Julia docs)
