Python-like slicing and handling UTF-8 strings as a bonus

Peter Alexander peter.alexander.au at gmail.com
Sat Dec 29 15:03:46 PST 2012


On Saturday, 29 December 2012 at 22:25:35 UTC, FG wrote:
> Forgive me if such a function already exists -- I couldn't find 
> it.

std.range have drop and take, which work on code points, not code 
units. They also handle over-dropping or over-taking gracefully. 
For example:

string s = "okrągły stół";
writeln(s.drop(8).take(3)); // "stó"
writeln(s.drop(8).take(100)); // "stół"
writeln(s.drop(100).take(100)); // ""

http://dpaste.dzfl.pl/2f8ebf49

It doesn't support negative indexing.

Generally speaking though, the vast majority of user code should 
never need to index into a Unicode string.


More information about the Digitalmars-d mailing list