www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is the correct way to test for an empty string?

reply "Gary Willoughby" <dev nomad.so> writes:
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?
Jul 16 2013
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
I just use

if(string.length == 0) {}

which covers both cases and is pretty intuitive too.
Jul 16 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Jul 16 2013
next sibling parent "Gary Willoughby" <dev nomad.so> writes:
On Tuesday, 16 July 2013 at 19:33:13 UTC, bearophile wrote:
 The right, safe and readable way is to use std.array.empty:

 if (myString.empty)
OMG, of course. Thanks!
Jul 16 2013
prev sibling parent reply "Rob T" <alanb ucora.com> writes:
On Tuesday, 16 July 2013 at 19:33:13 UTC, bearophile wrote:
 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
What was the rational for empty not being built in? Is there a performance penalty using "empty"? --rt
Jul 17 2013
next sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Wednesday, 17 July 2013 at 19:18:29 UTC, Rob T wrote:
 What was the rational for empty not being built in?

 Is there a performance penalty using "empty"?

 --rt
empty() was added to provide a range interface to arrays. Probably wasn't built in since you have to handle all array types. The concept of ranges make this clear, and since it didn't need to be built in to make it a range, it wasn't.
Jul 17 2013
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Jul 17, 2013 at 09:18:27PM +0200, Rob T wrote:
 On Tuesday, 16 July 2013 at 19:33:13 UTC, bearophile wrote:
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
What was the rational for empty not being built in? Is there a performance penalty using "empty"?
[...] AFAICT the reason is mainly historical. The whole deal with ranges was introduced rather late into the language, whereas array.length has always been there as a built-in feature. So .empty was layered on top of the language primitives after the fact, that's why it's in std.array instead of being built-in to the language. In a sense, it's a good thing to have only .length built-in; it simplifies the core language and the compiler, and allows more compile-time flexibility. OTOH, though, it might be a good idea to move parts of std.array into druntime's object.di so that things like .empty are available by default, rather than hiding in an obscure corner of Phobos. If ranges are such a big deal in D, as Walter seems to be pushing for, it makes sense to provide it by default in object.di. T -- Кто везде - тот нигде.
Jul 17 2013