D 如何从字符[]中删除元素?

D 如何从字符[]中删除元素?,d,D,我有一个带有字符的char[]。我想删除空白。我的做法: import std.algorithm; import std.ascii; // ... digits = remove!"isWhite(digits)"(digits); 但这不起作用: c:\dmd2\windows\bin\..\..\src\phobos\std\functional.d(70): Error: static assert "Bad unary function: isWhite(digits) for

我有一个带有字符的
char[]
。我想删除空白。我的做法:

import std.algorithm;
import std.ascii;
// ...
digits = remove!"isWhite(digits)"(digits);
但这不起作用:

c:\dmd2\windows\bin\..\..\src\phobos\std\functional.d(70): Error: static assert  "Bad unary function: isWhite(digits) for type dchar"
如何从
char[]
中删除所有空白

import std.algorithm;
import std.stdio;
import std.uni;
import std.array;

void main() {

    char[] s = "12 abc fg ".dup;

    writeln(array(s.filter!(x => !x.isWhite)));
}
需要删除
过滤器的
结果
-returntype。但您不必这样做,如果您想使用
范围

更经济的版本(不进行内存分配)是使用std.algorithm.remove这样(未经测试):


您最初尝试使用remove时,整个字符串使用了lambda,但它只需要一个字符对应一个字符。

谢谢
filter
也吸引了我的眼球,但我认为
remove
是正确的选择。我选择了filter,因为它与新的lambdas结合在一起是一个非常强大的工具(而且它与UFCS的结合看起来非常整洁)。例如,您可以一次删除所有空格、一组特殊字符、所有数字和字符
ABF
。只有将字符串转换为dchar[]:s=remove,我才能实现此功能!伊斯怀特(至!(dchar[])(s));
s = remove!isWhite(s);