# String getindex problem?

**URL:** https://discourse.julialang.org/t/string-getindex-problem/41743
**Category:** General Usage
**Created:** [June 19, 2020, 7:18pm UTC](https://discourse.julialang.org/t/string-getindex-problem/41743 "2020-06-19T19:18:55Z")
**Posts on this page:** 1
**Showing post:** 5

<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: [June 20, 2020, 1:37am UTC](https://discourse.julialang.org/t/string-getindex-problem/41743/5 "2020-06-20T01:37:40Z")

</div>

> [@Ronneesley](#):
>
> So, if I really want the ith position of a UTF-8 string,

The `i`-th codepoint is given by `s[nextind(s, 0, i)]`:

```julia
julia> s = "cão"
"cão"

julia> [s[nextind(s, 0, i)] for i = 1:3]
3-element Array{Char,1}:
 'c'
 'ã'
 'o'

```

However, realize that finding the `i`-th codepoint is O(i) (linear) complexity for the UTF-8 encoding or any variable-width encoding.

The real question is _why_ you want the `i`-th codepoint. Usually, random positions in strings arise from other processing, e.g. searches, in which the index is already computed as a byproduct.

As @johnmyleswhite alluded to, the notion of a “character” in Unicode might not be what you expect. The strings `s = "cão"` and `s2 = "cão"` may look the same, and are [canonically equivalent](https://en.wikipedia.org/wiki/Unicode_equivalence), but `s2` actually has 4 Unicode codepoints (“characters”) even though it has 3 graphemes (what most users would consider “characters”), because in `s2` the `ã` is made from an ASCII `a` followed by a [U+0303](https://www.fileformat.info/info/unicode/char/0303/index.htm) “combining tilde”. So, thinking in terms of the `i`-th “position” in a string may indicate a conceptual misunderstanding of Unicode.

---

_[View the full topic](https://discourse.julialang.org/t/string-getindex-problem/41743)._
