Empty string: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Not sure what that was there for...)
Line 10: Line 10:


[[Category:String manipulation]]
[[Category:String manipulation]]
=={{header|C}}==
In C the strings are <code>char</code> pointers. A string terminates with null char '\0', which is not considered part of the string. Thus an empty string is "\0", while a null string is a null pointer which points to nothing.
<lang C>/* to test a null string */
if (str) { ... }
/* to test if string is empty */
if (str[0] == '\0') { ... }
/* or equivelantly use strlen function */
if (strlen(str) == 0) { ... }
/* or compare to a known empty string, same thing. "== 0" means strings are equal */
if (strcmp(str, "") == 0) { ... }
</lang>
=={{header|D}}==
=={{header|D}}==
D treats null strings and empty strings as equal on the value level, but different on object level. You
D treats null strings and empty strings as equal on the value level, but different on object level. You

Revision as of 23:16, 4 July 2011

Empty string is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Some languages have syntax or semantics for dealing specifically with empty strings.

The task is to:

  • Demonstrate how to assign an empty string to a variable.
  • Demonstrate how to check that a string is empty
  • Demonstrate how to check that a string is not empty.

C

In C the strings are char pointers. A string terminates with null char '\0', which is not considered part of the string. Thus an empty string is "\0", while a null string is a null pointer which points to nothing. <lang C>/* to test a null string */ if (str) { ... } /* to test if string is empty */ if (str[0] == '\0') { ... } /* or equivelantly use strlen function */ if (strlen(str) == 0) { ... } /* or compare to a known empty string, same thing. "== 0" means strings are equal */ if (strcmp(str, "") == 0) { ... } </lang>

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>

Java

String.isEmpty() is part of Java 1.6. Other options for previous versions are noted. <lang java5>String s = ""; if(s != null && s.isEmpty()){//optionally, instead of "s.isEmpty()": "s.length() == 0" or "s.equals("")"

  System.out.println("s is empty");

}else{

  System.out.println("s is not empty");

}</lang>

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.

<lang perl6>my $s = ; say 'String is empty' unless $s.chars; say 'String is not empty' if $s.chars;</lang>

Python

<lang python>s = if not s:

   print('String s is empty.')

if s:

   print('String s is not empty.')</lang>