"strtok" D equivalent

Paul Backus snarwin at gmail.com
Thu Jul 28 23:16:15 UTC 2022


On Thursday, 28 July 2022 at 21:52:28 UTC, pascal111 wrote:
> On Thursday, 28 July 2022 at 20:36:31 UTC, Paul Backus wrote:
>> ```d
>> import std.algorithm: filter;
>> import std.range: empty;
>> import std.functional: not;
>>
>> // ...
>>
>>     auto tokens = input
>>         .splitter!(c => delimiters.canFind(c))
>>         .filter!(not!empty);
>>
>> // ...
>> ```
>
> I think "tokens" is a range. I didn't read much about it, but I 
> figured out that there's no particular way to know the number 
> of elements in a range, or how can you know the elements order 
> and the length of the range?

In this case, the only way is to convert the range to an array, 
using [`std.array.array`][1]:

```d
import std.array: array;

// ...

     string[] tokens = input
         .splitter!(c => delimiters.canFind(c))
         .filter!(not!empty)
         .array;
```

[1]: https://phobos.dpldocs.info/std.array.array.1.html


More information about the Digitalmars-d-learn mailing list