Empty string: Difference between revisions
Content added Content deleted
(added ocaml) |
m (→{{header|C}}: spelling, formatting) |
||
Line 11: | Line 11: | ||
[[Category:String manipulation]] |
[[Category:String manipulation]] |
||
=={{header|C}}== |
=={{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. |
In C the strings are <code>char</code> pointers. A string terminates with the null char (U+0000, <code>'\0'</code>), which is not considered part of the string. Thus an empty string is <code>"\0"</code>, while a null string is a null pointer which points to nothing. |
||
<lang C>/* to test a null string */ |
<lang C>/* to test a null string */ |
||
if (str) { ... } |
if (str) { ... } |
||
/* to test if string is empty */ |
/* to test if string is empty */ |
||
if (str[0] == '\0') { ... } |
if (str[0] == '\0') { ... } |
||
/* or |
/* or equivalently use strlen function */ |
||
if (strlen(str) == 0) { ... } |
if (strlen(str) == 0) { ... } |
||
/* or compare to a known empty string, same thing. "== 0" means strings are equal */ |
/* or compare to a known empty string, same thing. "== 0" means strings are equal */ |
||
if (strcmp(str, "") == 0) { ... } |
if (strcmp(str, "") == 0) { ... } |
||
</lang> |
</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 |