import std.utf; /* Slice UTF-8 string like it do string[from..to] for ASCII; Use negative index to access string from end */ string slice(in string a, int from, int to) { int getIndexOfSymbol(in string a, int pos) { int res; if (pos < 0) { pos += std.utf.count(a); } for (; pos > 0; pos--) { res += a.stride(res); } return res; } return a[getIndexOfSymbol(a, from)..getIndexOfSymbol(a, to)]; } unittest { string ascii = "Hello"; assert(slice(ascii, 2, 3) == ascii[2..3]); assert(slice(ascii, 2, -1) == ascii[2..$]); assert(slice(ascii, 2, -2) == ascii[2..$-1]); assert(slice(ascii, 0, 3) == ascii[0..3]); assert(slice(ascii, -2, -1) == ascii[$-1..$]); assert(slice("Привет", 2, 3) == "ив"); assert(slice("Привет", 2, -1) == "ивет"); assert(slice("Привет", 0, 3) == "Прив"); assert(slice("Привет", -2, -1) == "ет"); }