# Indexing Unicode Strings

**URL:** https://discourse.julialang.org/t/indexing-unicode-strings/62325
**Category:** Internals & Design
**Created:** [June 3, 2021, 9:37am UTC](https://discourse.julialang.org/t/indexing-unicode-strings/62325 "2021-06-03T09:37:42Z")
**Posts on this page:** 1
**Showing post:** 4

<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 3, 2021, 12:11pm UTC](https://discourse.julialang.org/t/indexing-unicode-strings/62325/4 "2021-06-03T12:11:44Z")

</div>

> [@karlroyen](#):
>
> One usecase I actually had yesterday was that I wanted to clip strings to 10 characters for compact printing.

“Character” (= codepoint) doesn’t mean what you think in Unicode, and your solution for this is buggy. Some Unicode codepoints are 0 characters wide, and some are 2 characters wide. For example, `"föó"` is 5 codepoints, 2 of which are “modifier” characters of 0 width (which add accents to the preceding character). (Moreover, the canonically equivalent string `"föó"` has 3 codepoints! See [here](https://discourse.julialang.org/t/string-getindex-problem/41743/5) if this confuses you.)

A better solution would be to use `textwidth`, which measures the width of strings and characters (approximately, because in some cases this depends on the font and the terminal). For example:

```julia
function clipwidth(s::AbstractString, maxwidth::Integer)
    width = 0
    for (i,c) in pairs(s)
        width += textwidth(c)
        width > maxwidth && return s[1:prevind(s, i)]
    end
    return s
end

```

In general, thinking of “character indices” in Unicode is very often a sign of misunderstanding Unicode, and in that sense Julia’s string indexing has the helpful side effect of catching a lot of bugs.

PS. That being said, you can get the n-th index of a string `s` with `nextind(s,0,n)`, so you can do `text[1:nextind(text,0,n)]` to obtain the first `n` Unicode characters (codepoints) if that is really what you want.

PPS. Also, I think `@printf` got it wrong here: [julia#41068](https://github.com/JuliaLang/julia/issues/41068).

---

_[View the full topic](https://discourse.julialang.org/t/indexing-unicode-strings/62325)._
