D Language Strings Test for an empty or null string


Example

Empty string

Empty string is not null but has zero length:

string emptyString = "";
// an empty string is not null...
assert(emptyString !is null);

// ... but it has zero lenght
assert(emptyString.length == 0);

Null string

string nullString = null;

a null string is null (De Lapalisse)

assert(nullString is null);

but, unlike C#, read the length of a null string does not generate error:

assert(nullString.length == 0);
assert(nullString.empty);

Test for empty or null


if (emptyOrNullString.length == 0) {
}

// or
if (emptyOrNullString.length) {
}

// or
import std.array;
if (emptyOrNullString.empty) {
}

Test for null

if (nullString is null) {
}

References