On Saturday, 22 October 2022 at 21:53:05 UTC, WhatMeWorry wrote:


string[] tokens = userSID.output.split!isWhite;
writeln("tokens = ", tokens); 
[...]
Is there a clever way that I can discard all the extra null strings in the resultant string array?

Easiest way is to use [`filter`][1]. Here's an example:

```d
import std.algorithm: splitter, filter;
import std.uni: isWhite; // or use std.ascii for non-unicode input
import std.array: array;
import std.stdio: writeln;

string exampleText =
    "Hello           123-456-ABC    x\ny\tz\r\nwvu   goodbye";

void main()
{
        string[] tokens = exampleText
        .splitter!isWhite
        .filter!(t => t.length > 0)
        .array;
        writeln("tokens = ", tokens);
}
```

I've also used the lazily-evaluated [`splitter`][2] instead of the eagerly-evaluated `split`, to avoid allocating a temporary array unnecessarily.

[1]: https://phobos.dpldocs.info/std.algorithm.iteration.filter.html [2]: https://phobos.dpldocs.info/std.algorithm.iteration.splitter.3.html

Reply via email to