Empty string: Difference between revisions

(→‎{{header|Perl 6}}: Add Perl 6 example)
(→‎{{header|D}}: added D)
Line 10:
 
[[Category:String manipulation]]
=={{header|D}}==
D treats null strings and empty strings as equal on the value level, but different on object level. You
need to take this into account when checking for empty.
<lang d>void main(){
string s1 = null;
string s2 = "";
// the content is the same
assert(!s1.length);
assert(!s2.length);
assert(s1 == "" && s1 == null);
assert(s2 == "" && s2 == null);
assert(s1 == s2);
 
// but they don't point to the same memory region
assert(s1 is null && s1 !is "");
assert(s2 is "" && s2 !is null);
assert(s1 !is s2);
assert(s1.ptr == null);
assert(*s2.ptr == '\0');
assert(isEmpty(s1));
assert(isEmptyNotNull(s2));
}
 
bool isEmpty(string s) {
return !s.length;
}
 
bool isEmptyNotNull(string s) {
return s is "";
}</lang>
 
=={{header|Java}}==
<code>String.isEmpty()</code> is part of Java 1.6. Other options for previous versions are noted.
Line 18 ⟶ 51:
System.out.println("s is not empty");
}</lang>
--[[User:Fwend|Fwend]] 22:51, 4 July 2011 (UTC)
 
=={{header|Perl 6}}==
In Perl 6 we can't just test a string for truth to determine if it has a value. The string "0" will test as false even though it has a value. Instead we must test for length.
Anonymous user