Empty string: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 445:
/* or compare to a known empty string, same thing. "== 0" means strings are equal */
if (strcmp(str, "") == 0) { ... }
</lang>
 
=={{header|C++}}==
<lang cpp>#include <string>
 
// ...
 
 
// empty string declaration
std::string str; // (default constructed)
std::string str(); // (default constructor, no parameters)
std::string str{}; // (default initialized)
std::string str(""); // (const char[] conversion)
std::string str{""}; // (const char[] initializer list)
 
 
 
if (str.empty()) { ... } // to test if string is empty
 
// we could also use the following
if (str.length() == 0) { ... }
if (str == "") { ... }
 
// make a std::string empty
str.clear(); // (builtin clear function)
str = ""; // replace contents with empty string
str = {}; // swap contents with temp string (empty),then destruct temp
 
// swap with empty string
std::string tmp{}; // temp empty string
str.swap(tmp); // (builtin swap function)
std::swap(str, tmp); // swap contents with tmp
 
 
// create an array of empty strings
std::string s_array[100]; // 100 initialized to "" (fixed size)
std::array<std::string, 100> arr; // 100 initialized to "" (fixed size)
std::vector<std::string>(100,""); // 100 initialized to "" (variable size, 100 starting size)
 
// create empty string as default parameter
void func( std::string& s = {} ); // {} generated default std:string instance
</lang>
 
Line 618 ⟶ 577:
}</lang>
 
=={{header|DyalectC++}}==
<lang cpp>#include <string>
 
// ...
Demonstrate how to assign an empty string to a variable:
 
<lang dyalect>var str = ""</lang>
 
// empty string declaration
Demonstrate how to check that a string is empty:
std::string str; // (default constructed)
std::string str(); // (default constructor, no parameters)
std::string str{}; // (default initialized)
std::string str(""); // (const char[] conversion)
std::string str{""}; // (const char[] initializer list)
 
<lang dyalect>if !str { }
//or
if str.isEmpty() { }</lang>
 
Demonstrate how to check that a string is not empty:
 
if (str.empty()) { ... } // to test if string is empty
<lang dyalect>if str { }
 
//or
// we could also use the following
if !str.isEmpty() { }</lang>
if (str.length() == 0) { ... }
if (str == "") { ... }
 
// make a std::string empty
str.clear(); // (builtin clear function)
str = ""; // replace contents with empty string
str = {}; // swap contents with temp string (empty),then destruct temp
 
// swap with empty string
std::string tmp{}; // temp empty string
str.swap(tmp); // (builtin swap function)
std::swap(str, tmp); // swap contents with tmp
 
 
// create an array of empty strings
std::string s_array[100]; // 100 initialized to "" (fixed size)
std::array<std::string, 100> arr; // 100 initialized to "" (fixed size)
std::vector<std::string>(100,""); // 100 initialized to "" (variable size, 100 starting size)
 
// create empty string as default parameter
void func( std::string& s = {} ); // {} generated default std:string instance
</lang>
 
=={{header|Caché ObjectScript}}==
Line 801 ⟶ 783:
}
}</lang>
 
=={{header|Déjà Vu}}==
Like in Python, empty strings are falsy, non-empty strings are truthy.
<lang dejavu>local :e ""
 
if not e:
!print "an empty string"
 
if e:
!print "not an empty string"</lang>
 
=={{header|Delphi}}==
Line 846 ⟶ 818:
if s <> '' then
PrintLn('not empty');</lang>
 
=={{header|Dyalect}}==
 
Demonstrate how to assign an empty string to a variable:
 
<lang dyalect>var str = ""</lang>
 
Demonstrate how to check that a string is empty:
 
<lang dyalect>if !str { }
//or
if str.isEmpty() { }</lang>
 
Demonstrate how to check that a string is not empty:
 
<lang dyalect>if str { }
//or
if !str.isEmpty() { }</lang>
 
=={{header|Déjà Vu}}==
Like in Python, empty strings are falsy, non-empty strings are truthy.
<lang dejavu>local :e ""
 
if not e:
!print "an empty string"
 
if e:
!print "not an empty string"</lang>
 
=={{header|EasyLang}}==
Line 935:
-- string is not empty
end if</lang>
 
 
=={{header|F_Sharp|F#}}==
Line 1,027 ⟶ 1,026:
 
With F90, compound data aggregates can be defined and as well procedures for operating on them, so that, after a great deal of syntactic struggle, a string data type will be available. F2000 standardised one such scheme whereby character variables are de-allocated and re-allocated with usage so that a statement such as <code>TEXT = "This" // "That"</code> would cause a de-allocation of whatever storage had been associated with TEXT followed by a re-allocation of storage for eight characters, the required size, and LEN(TEXT) would give 8.
 
=={{header|Free Pascal}}==
Assigning an empty string:
<lang pascal>s := '';</lang>
Checking for an empty string:
<lang pascal>s = ''
length(s) = 0</lang>
Checking for a non-empty string:
<lang pascal>s <> ''
length(s) > 0
longBool(length(s))</lang>
The <code>sysUtils</code> unit defines the constants <code>emptyStr</code> and <code>emptyWideStr</code>, which can be used in place of <code>&#39;&#39;</code>.
 
=={{header|FreeBASIC}}==
Line 1,053 ⟶ 1,064:
String is not empty
</pre>
 
=={{header|Free Pascal}}==
Assigning an empty string:
<lang pascal>s := '';</lang>
Checking for an empty string:
<lang pascal>s = ''
length(s) = 0</lang>
Checking for a non-empty string:
<lang pascal>s <> ''
length(s) > 0
longBool(length(s))</lang>
The <code>sysUtils</code> unit defines the constants <code>emptyStr</code> and <code>emptyWideStr</code>, which can be used in place of <code>&#39;&#39;</code>.
 
=={{header|FutureBasic}}==
Line 1,247 ⟶ 1,246:
System.out.println("s is not empty");
}</lang>
 
 
=={{header|JavaScript}}==
Line 1,890 ⟶ 1,888:
<lang Oforth>"" isEmpty
"" isEmpty not</lang>
 
=={{header|OpenEdge/Progress}}==
Strings can be stored in CHARACTER and LONGCHAR variables. Both are initially empty. Both can also be unknown. A CHARACTER has a maximum length of approx 32000 bytes.
 
<lang progress>DEFINE VARIABLE cc AS CHARACTER.
 
IF cc > '' THEN
MESSAGE 'not empty' VIEW-AS ALERT-BOX.
ELSE IF cc = ? THEN
MESSAGE 'unknown' VIEW-AS ALERT-BOX.
ELSE /* IF cc = '' */
MESSAGE 'empty' VIEW-AS ALERT-BOX.
</lang>
 
=={{header|Ol}}==
Line 1,937 ⟶ 1,922:
Variable w does not contain the empty string
this is not a good test</pre>
 
=={{header|OpenEdge/Progress}}==
Strings can be stored in CHARACTER and LONGCHAR variables. Both are initially empty. Both can also be unknown. A CHARACTER has a maximum length of approx 32000 bytes.
 
<lang progress>DEFINE VARIABLE cc AS CHARACTER.
 
IF cc > '' THEN
MESSAGE 'not empty' VIEW-AS ALERT-BOX.
ELSE IF cc = ? THEN
MESSAGE 'unknown' VIEW-AS ALERT-BOX.
ELSE /* IF cc = '' */
MESSAGE 'empty' VIEW-AS ALERT-BOX.
</lang>
 
=={{header|PARI/GP}}==
Line 1,971 ⟶ 1,969:
$s = "0 but true";
if ($s) { ... } # it's true! black magic!</lang>
 
=={{header|Perl 6}}==
 
<lang perl6>my $s = '';
say 'String is empty' unless $s;
say 'String is not empty' if $s;</lang>
 
Unlike in Perl 5, only empty strings test as false - the string "0" tests as true now.
 
=={{header|Phix}}==
Line 2,032 ⟶ 2,022:
if length(s) > 0 then ... /* to test for a non-empty string */
</lang>
 
 
=={{header|PowerShell}}==
Line 2,138 ⟶ 2,127:
(define (string-null? s) (string=? "" s))
(define (string-not-null? s) (string<? "" s))</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<lang perl6>my $s = '';
say 'String is empty' unless $s;
say 'String is not empty' if $s;</lang>
 
Unlike in Perl 5, only empty strings test as false - the string "0" tests as true now.
 
=={{header|Rascal}}==
10,333

edits