# Weird string slicing in korean

**URL:** <https://discourse.julialang.org/t/weird-string-slicing-in-korean/92252>\
**Category:** Performance\
**Created:** [December 29, 2022, 1:17am UTC](https://discourse.julialang.org/t/weird-string-slicing-in-korean/92252 "2022-12-29T01:17:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmsmsgood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmsmsgood/32/20544_2.png) [@rmsmsgood](https://discourse.julialang.org/u/rmsmsgood)\
**Post date:** [December 29, 2022, 1:17am UTC](https://discourse.julialang.org/t/weird-string-slicing-in-korean/92252/1 "2022-12-29T01:17:01Z")

</div>

I have a trouble with korean string:

```julia
julia> julia = "julia"
"julia"

julia> julia[1:2]
"ju"

julia> julia = "줄리아"
"줄리아"

julia> julia[1:2]
ERROR: StringIndexError: invalid index [2], valid nearby indices [1]=>'줄', [4]=>'리'
Stacktrace:
 [1] string_index_err(s::String, i::Int64)
   @ Base .\strings\string.jl:12
 [2] getindex(s::String, r::UnitRange{Int64})
   @ Base .\strings\string.jl:267
 [3] top-level scope
   @ c:\Users\rmsms\OneDrive\lab\Rt\preprocess.jl:33

julia> julia[1:4]
"줄리"

```

Yes I could get why it works in this weird way(Maybe because korean character have 2 byte?), and I can handle it now. But Is this intentional?

ps. Japanese and Chinese character have same issue:

```julia
julia> julia = "ジュリア"
"ジュリア"

julia> julia[1:2]
ERROR: StringIndexError: invalid index [2], valid nearby indices [1]=>'ジ', [4]=>'ュ'
Stacktrace:
 [1] string_index_err(s::String, i::Int64)
   @ Base .\strings\string.jl:12
 [2] getindex(s::String, r::UnitRange{Int64})
   @ Base .\strings\string.jl:267
 [3] top-level scope
   @ c:\Users\rmsms\OneDrive\lab\Rt\preprocess.jl:37

julia> julia[1:4]
"ジュ"

julia> julia = "酒利兒"
"酒利兒"

julia> julia[1:2]
ERROR: StringIndexError: invalid index [2], valid nearby indices [1]=>'酒', [4]=>'利'
Stacktrace:
 [1] string_index_err(s::String, i::Int64)
   @ Base .\strings\string.jl:12
 [2] getindex(s::String, r::UnitRange{Int64})
   @ Base .\strings\string.jl:267
 [3] top-level scope
   @ c:\Users\rmsms\OneDrive\lab\Rt\preprocess.jl:41

julia> julia[1:4]
"酒利"

```

---

<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:** [December 29, 2022, 1:36am UTC](https://discourse.julialang.org/t/weird-string-slicing-in-korean/92252/2 "2022-12-29T01:36:58Z")

</div>

this is intentional. strings are indexed by bytes for performance reasons

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 29, 2022, 1:52am UTC](https://discourse.julialang.org/t/weird-string-slicing-in-korean/92252/3 "2022-12-29T01:52:39Z")

</div>

If you don’t have pressing performance concerns, you could do

```julia
julia> String(collect("줄리아")[1:2])
"줄리"

```

which takes about 70 ns.

---

<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:** [December 29, 2022, 2:50am UTC](https://discourse.julialang.org/t/weird-string-slicing-in-korean/92252/4 "2022-12-29T02:50:06Z")

</div>

See also this discussion:

> [@Substring function?](https://discourse.julialang.org/t/substring-function/76675/27):
>
> Not efficiently. The basic argument is that a variable-width encoding with non-consecutive codepoint indices is a good tradeoff to make (memory efficiency + speed, at the cost of less-intuitive indexing) because “give me the m-th codepoint” or “or give me the substring from codepoints m to n” is extremely uncommon in (correct) string-handling code, as opposed to “give me the substring at opaque indices I found in a previous search/loop”. That’s why, [in my previous post](https://discourse.julialang.org/t/substring-function/76675/10), I asked you where your…
