What is the correct way to test for an empty string?

bearophile bearophileHUGS at lycos.com
Tue Jul 16 12:33:12 PDT 2013


Gary Willoughby:

> What is the correct way to test for an empty string?
>
> I've used
>
> if (string == "")
>
> and
>
> if (string is null)
>
> and both (O_o) in some places, it's starting to do my head in. 
> What is the correct way?

The right, safe and readable way is to use std.array.empty:

if (myString.empty)

If you don't want to import functions, then test for the length:

if (string.length == 0)

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list