Empty string vs null
mark
mark at qtrac.eu
Tue Feb 4 07:33:42 UTC 2020
I have just discovered that D seems to treat empty and null
strings as the same thing:
// test.d
import std.stdio;
import std.string;
void main()
{
string x = null;
writeln("x = \"", x, "\"");
writeln("null = ", x == null);
writeln("\"\" = ", x == "");
writeln("empty = ", x.empty);
x = "";
writeln("\nx = \"", x, "\"");
writeln("null = ", x == null);
writeln("\"\" = ", x == "");
writeln("empty = ", x.empty);
x = "x";
writeln("\nx = \"", x, "\"");
writeln("null = ", x == null);
writeln("\"\" = ", x == "");
writeln("empty = ", x.empty);
}
Output:
x = ""
null = true
"" = true
empty = true
x = ""
null = true
"" = true
empty = true
x = "x"
null = false
"" = false
empty = false
1. Why is this?
2. Should I prefer null or ""? I was hoping to return null to
indicate "no string that match the criteria", and "some string"
otherwise.
More information about the Digitalmars-d-learn
mailing list