# Substring function?

**URL:** <https://discourse.julialang.org/t/substring-function/76675>\
**Category:** New to Julia\
**Tags:** strings, unicode\
**Created:** [February 18, 2022, 7:41am UTC](https://discourse.julialang.org/t/substring-function/76675 "2022-02-18T07:41:49Z")\
**Posts on this page:** 3\
**Page:** 3

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [July 18, 2022, 2:37pm UTC](https://discourse.julialang.org/t/substring-function/76675/41 "2022-07-18T14:37:33Z")

</div>

> [@davidavdav](#):
>
> But then, what is the _real_ application of byte-indexing a substring?

Iterating over letters (code points) of a string gives you byte indexes I believe (just not necessarily consecutive), so at some (lower-level, yes) point you need byte-indexing, e.g. for `SubString`s. I would have liked indexing (byte-based or otherwise) to not be defined on `String`s (by default), but by now that would be a breaking change. That’s my plan for a string-type I’m developing…

One other application is, strings in Julia do not have to hold UTF-8 data (but you can optionally validate as such). It could either be e.g. byte-based ISO-8859-1 or any kind of binary data (even UTF-16), or malformed UTF-8. So I suppose if you know you’re dealing with byte-based you might want byte-indexing (as opposed to the provided iterator)… or every other byte for UTF-16 (for code-points, not letters, recall UTF-16 is also variable-length because of surrogates).

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 18, 2022, 2:44pm UTC](https://discourse.julialang.org/t/substring-function/76675/42 "2022-07-18T14:44:03Z")

</div>

The reason byte indexes are useful is that they are the thing that can be looked up in O(1) so if you search for graphemes, and return bytes, it makes it easy to quickly parse through a string.

---

<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:** [July 18, 2022, 4:41pm UTC](https://discourse.julialang.org/t/substring-function/76675/43 "2022-07-18T16:41:00Z")

</div>

> [@Oscar\_Smith](#):
>
> The reason byte indexes are useful is that they are the thing that can be looked up in O(1)

In particular, you should think of the byte (codeunit) index as simply an opaque “pointer” into the string which is returned by iteration, searching, etcetera. The user doesn’t need to understand how it relates to the encoding. The important thing is that once you have this opaque pointer, you can jump to that position quickly (O(1)).

Put another way, _why have indices at all_? The reason for them is to _save locations in a string_. But you don’t necessarily need to know what those locations “mean” as long as you can do various things with them (e.g. reading at a location, comparing two locations by ≤ or \<, grabbing a substring between two locations, …).

> [@davidavdav](#):
>
> In my real applications I sometimes use NFD, when I am interested in diacritics, and otherwise NFC.

There is a common misconception lurking — normalization is _not_ sufficient to merge all graphemes into single characters. Unicode has “merged” characters that include the accent only for a small subset of characters and diacritical marks; everything else will use a combining character _even in NFC_.

For example, the string `"x̂"` contains only one grapheme (one “user-visible” character) but is two characters (two Unicode codepoints) in _any_ normalization.

[Previous page](https://discourse.julialang.org/t/substring-function/76675.md?page=2)
