Note that the indices are byte indices not character indices. You probably should use the nextind function to get what you want for general UTF-8 strings.
What I want to say is that in Julia there is a difference between character indices and byte indices.
Only if your data is only ASCII they are equivalent. In your example they are, so in such case you are safe to write what you have specified.
If you have UTF-8 data you first have to tell me if you use byte indices (probably not) or character indices (probably yes). In the latter case you should to use functions that operate on characters like nextind, first, last or chop. In your case you could write for example last(first(s, 7), 3) to get a 3 character string consisting of characters 5, 6 and 7 from the original string. This is not the most efficient way to do it, but it is simplest.
More efficient approach would use nextind to calculate byte index of first and last character in your string and do SubString using these indices.
Also you can use eachindex on string to get an iterator of byte indices that correspond to consecutive characters in the string. Here is an example (along with some performance benchmarks of different methods proposed):
function charsub(x::AbstractString, from_char, to_char)
from_idx = 0
to_idx = 0
for (i, idx) in enumerate(eachindex(x))
i == from_char && (from_idx = idx)
i == to_char && (to_idx = idx; break)
end
SubString(x, from_idx, to_idx)
end
and the benchmarks:
julia> using Random, BenchmarkTools
julia> x = randstring(20)
"vfRVI4vxz4RWKu8VBv1E"
julia> @btime string([c for c in $x][5:7]...)
194.601 ns (4 allocations: 304 bytes)
"I4v"
julia> @btime last(first($x, 7), 3)
72.986 ns (2 allocations: 64 bytes)
"I4v"
julia> @btime charsub($x, 5, 7)
51.919 ns (2 allocations: 48 bytes)
"I4v"