# StringIndex idea (Julia 2.0)

**URL:** https://discourse.julialang.org/t/stringindex-idea-julia-2-0/107185
**Category:** Internals & Design
**Tags:** strings, indexing
**Created:** [December 5, 2023, 8:23pm UTC](https://discourse.julialang.org/t/stringindex-idea-julia-2-0/107185 "2023-12-05T20:23:43Z")
**Posts on this page:** 1
**Showing post:** 68

<div class="post-metadata">

### Author: ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)
#### Post date: [February 9, 2024, 3:51pm UTC](https://discourse.julialang.org/t/stringindex-idea-julia-2-0/107185/68 "2024-02-09T15:51:12Z")

</div>

I wouldn’t want to conflate ordinal syntax with `StringIndex`, although I think you’re on to something with using juxtaposition for `StringIndex` types.

Two reasons. The first is that indexing by codepoint isn’t an ordinal vs. cardinal distinction, so “If you use `4`, it’s a byte index, if you use `4th` it’s a codepoint index” would just be something to remember, it’s not at all obvious that `4th` means the fourth codepoint, not the fourth codeunit.

The other one is that there are at minimum four ways to want to index a string: by its codeunits, codepoints, graphemes, and textwidth. I would argue that _most times_ a developer is tempted to use codepoints, what they actually want is graphemes. Codepoints will prevent throwing an invalid index error, but they don’t prevent splitting up 👍🏼 or 🇦🇺 or Â (I don’t have a good way to type Latin characters with combining codepoints, just pretend that one is composed).

But a syntax such as `string[1ch:5ch]`, `string[1gr:5gr]`, `string[1wc:5wc]` is a nicely-compact way to generate those sorts of index. `wc` because of the `wcwidth` function which never quite made it into the C standard, it could be `1tw` to remind users that it’s using the `textwidth` function under the hood. I think this is fitting because the various approaches to indexing are effectively units, and this looks like a unit, and units are a place where highly-abbreviated names are considered acceptable, no one insists on `1m` being spelled `1meter`.

If this system also had `CharString` `GraphemeString` etc., the set should include `1cu` (maybe just `1u`?), so those can be addressed by a codeunit, since each would have a default interpretation of an `Int`. In fact, maybe _that_ is the place to unify with ordinal indexing, since it carries the intended meaning of “index this in the normal way, whether or not that differs from whatever weird expectation the indexable type has for a number”.

---

_[View the full topic](https://discourse.julialang.org/t/stringindex-idea-julia-2-0/107185)._
