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

FG home at fgda.pl
Sat Dec 29 16:02:17 PST 2012


On 2012-12-30 00:03, Peter Alexander wrote:
> 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)); // ""
>

Ah, so this is the way of doing it. Thanks.


> It doesn't support negative indexing.

At least dropping off the back is also possible s[2..$-5]:

     writeln(s.retro.drop(5).retro.drop(2)); // "rągły"

     (or with dropBack, without retro, if available)

I have no idea how to do s[$-4..$-2] though.



More information about the Digitalmars-d mailing list