D2:条件语句中的空字符串

fwe*_*end 3 d

在下面的代码中,为什么2给出输出但不给出3?removechars语句返回长度为0的字符串

import std.stdio, std.string;

void main() {
    string str = null;
    if (str) writeln(1); // no

    str = "";
    if (str) writeln(2); // yes

    if (",&%$".removechars(r"^a-z"))  writeln(3); // no
}
Run Code Online (Sandbox Code Playgroud)

编辑:好吧,它可能会返回null,但我仍然有点困惑,因为所有这些打印都是真的

writeln(",&%$".removechars(r"^a-z") == "");
writeln(",&%$".removechars(r"^a-z") == null);
writeln(",&%$".removechars(r"^a-z").length == 0);
Run Code Online (Sandbox Code Playgroud)

编辑2:这也打印为true,但是将它们中的任何一个置于条件中并获得不同的结果

writeln("" == null);
Run Code Online (Sandbox Code Playgroud)

编辑3:好吧,我明白我不能像我那样测试空字符串.导致这个问题的是以下情况.我想从单词中删除字符,但不想存储空字符串:

if (auto w = word.removechars(r"^a-z"))
    wordcount[w]++;
Run Code Online (Sandbox Code Playgroud)

这在我尝试时有效,但这必须是因为removechars返回null而不是""

Vla*_*eev 5

因为没有字符匹配时removeChars会返回null.

(这是因为.dup一直是空字符串null.)