Iterating chars by Word
Виталий Фадеев
vital.fadeev at gmail.com
Fri Nov 13 06:52:38 UTC 2020
On Friday, 13 November 2020 at 06:42:24 UTC, evilrat wrote:
> On Friday, 13 November 2020 at 05:14:08 UTC, Виталий Фадеев
> wrote:
>> [...]
>
> You can make your own range, however look at this function
> first (second example)
> https://dlang.org/phobos/std_algorithm_iteration.html#.splitter
>
> // 1) might need to cast your wchar[] to wstring first
> // 2) also assumes that 'the word' is separated by
> whitespace
> foreach( word; chars.splitter(' '))
> {
>
> }
>
> or this one, which is a bit more smarter about what "the word"
> means
> https://dlang.org/phobos/std_array.html#.split
>
> import std.array : split;
>
> wchar[] str = cast(wchar[]) "some random stuff blah blah"w;
> foreach(w; str.split())
> {
> writeln(w);
> }
>
> Anyway in both cases using dmd -vgc flag shows no GC
> allocations done.
Thanks, Ali. Thanks, Evilrat.
I taste it now: https://run.dlang.io/is/HlSFVY
More information about the Digitalmars-d-learn
mailing list