Checking if a string is null

Max Samukha samukha at voliacable.com.removethis
Wed Jul 25 01:52:48 PDT 2007


On Wed, 25 Jul 2007 08:32:52 +0200, Hoenir <mrmocool at gmx.de> wrote:

>Max Samukha schrieb:
>> Using '== null' and 'is null' with strings gives odd results (DMD
>> 1.019):
>> 
>> void main()
>> {
>> 	char[] s;
>> 
>> 	if (s is null) writefln("s is null");
>> 	if (s == null) writefln("s == null");		
>> }
>> 
>> Output:
>> s is null
>> s == null
>> 
>> ----
>> 
>> void main()
>> {
>> 	char[] s = "";
>> 
>> 	if (s is null) writefln("s is null");
>> 	if (s == null) writefln("s == null");		
>> }
>> 
>> Output:
>> s == null
>> 
>> ----
>> 
>> Can anybody explain why s == null is true in the second example?
>> 
>Makes sense to me. is compares the pointer and == the content or 
>something like that.

Then, it's unclear what null content means. If it is the same as empty
string (ptr != null and length == 0), I remain confused. If it means a
null string (ptr == null and length == 0), the second example should
output nothing since s.ptr != null.


More information about the Digitalmars-d-learn mailing list