Empty string: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 16:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">V s = ‘’
I s.empty
print(‘String s is empty.’)
I !s.empty
print(‘String s is not empty.’)</langsyntaxhighlight>
 
=={{header|6502 Assembly}}==
An empty string is just a null terminator with no text in front.
<langsyntaxhighlight lang="6502">EmptyString:
byte 0</langsyntaxhighlight>
 
Checking if a string is empty is simple, just count the number of characters before you reach the terminator. If that count equals zero, the string is empty. Otherwise, the string is not empty.
 
<langsyntaxhighlight lang="6502asm">lda #<EmptyString ;address of string we wish to check
sta $00
lda #>EmptyString
Line 44:
cpy #0
beq StringIsEmpty ;if this branch is taken, the string is empty
;otherwise, the string is not empty</langsyntaxhighlight>
 
=={{header|68000 Assembly}}==
{{trans|6502 Assembly}}
An empty string is just a null terminator with no text in front.
<langsyntaxhighlight lang="68000devpac">EmptyString:
DC.B 0
EVEN</langsyntaxhighlight>
Checking if a string is empty is simple, just count the number of characters before you reach the terminator. If that count equals zero, the string is empty. Otherwise, the string is not empty.
<langsyntaxhighlight lang="68000devpac">LEA EmptyString,A0 ;assign the empty string to address register A0
 
getStringLength:
Line 66:
CMP.L A0,(SP) ;compare the current A0 with the original value.
BEQ StringIsEmpty ;if they are equal, then nothing was read besides the terminator. Therefore the string is empty.
;if the above branch wasn't taken, the string is not empty and execution arrives here.</langsyntaxhighlight>
 
=={{header|8th}}==
Assign an empty string to a variable:
<langsyntaxhighlight Forthlang="forth">"" var, str</langsyntaxhighlight>
 
Check that the string is empty:
<langsyntaxhighlight Forthlang="forth">str @ s:len 0 n:= if ... then</langsyntaxhighlight>
 
The check for a non-empty string is the same, but with "not" after the n:=
Line 81:
Declare an empty string at address <code>str</code>:
 
<langsyntaxhighlight ARM_Assemblylang="arm_assembly">str: .asciz ""</langsyntaxhighlight>
 
Check if a string stored at <code>x0</code> is empty:
 
<langsyntaxhighlight ARM_Assemblylang="arm_assembly"> mov x5, #0
ldrb w5, [x0]
cmp x5, #0</langsyntaxhighlight>
 
Full program demo:
Line 93:
{{works with|aarch64-linux-gnu-as/qemu-aarch64}}
 
<langsyntaxhighlight ARM_Assemblylang="arm_assembly">.equ STDOUT, 1
.equ SVC_WRITE, 64
.equ SVC_EXIT, 93
Line 148:
_exit:
mov x8, #SVC_EXIT
svc #0</langsyntaxhighlight>
 
=={{header|ACL2}}==
To check if a string is empty:
<langsyntaxhighlight Lisplang="lisp">(= (length str) 0)</langsyntaxhighlight>
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC CheckIsEmpty(CHAR ARRAY s)
PrintF("'%S' is empty? ",s)
IF s(0)=0 THEN
Line 172:
CheckIsEmpty(str1)
CheckIsEmpty(str2)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Empty_string.png Screenshot from Atari 8-bit computer]
Line 182:
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">procedure Empty_String is
 
function Is_Empty(S: String) return Boolean is
Line 196:
raise Program_Error with "something went wrong very very badly!!!";
end if;
end Empty_String;</langsyntaxhighlight>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">text s;
s = "";
if (length(s) == 0) {
Line 206:
if (length(s) != 0) {
....
}</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<langsyntaxhighlight lang="algol68"># declare a string variable and assign an empty string to it #
STRING s := "";
 
Line 221:
# as a string is an array of characters, we could also test for emptyness by #
# checking for lower bound > upper bound #
IF LWB s > UPB s THEN write( ( "s is still empty", newline ) ) FI</langsyntaxhighlight>
 
=={{header|Apex}}==
<syntaxhighlight lang="apex">
<lang Apex>
String.isBlank(record.txt_Field__c);
--Returns true if the specified String is white space, empty (''), or null; otherwise, returns false.
</syntaxhighlight>
</lang>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">
<lang APL>
⍝⍝ Assign empty string to A
A ← ''
Line 237:
~0 = ⍴∊ A
0
</syntaxhighlight>
</lang>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">
<lang AppleScript>
-- assign empty string to str
set str to ""
Line 271:
-- str is not empty
end if
</syntaxhighlight>
</lang>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 361:
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">s: ""
 
if empty? s -> print "the string is empty"
Line 373:
 
if not? empty? s -> print "the string is not empty"
if 0 < size s -> print "no, the string is not empty"</langsyntaxhighlight>
 
{{out}}
Line 383:
 
=={{header|Asymptote}}==
<langsyntaxhighlight Asymptotelang="asymptote">string c; //implicitly assigned an empty string
if (length(c) == 0) {
write("Empty string");
Line 403:
} else {
write("Empty string");
}</langsyntaxhighlight>
{{out}}
<pre>Empty string
Line 412:
AutoHotkey has both "Traditional" or literal text, and "Expression" mode.
This code demonstrates the task using both methods.
<langsyntaxhighlight AutoHotkeylang="autohotkey">;; Traditional
; Assign an empty string:
var =
Line 431:
; Check that a string is not empty
If (var != "")
Msgbox the var is not empty</langsyntaxhighlight>
 
=={{header|Avail}}==
The type string is defined as <code><character…|></code> (a tuple of characters), so the empty tuple and the empty string are equivalent values, and all tuple/collection methods can be used on strings.
 
<langsyntaxhighlight Availlang="avail">emptyStringVar : string := "";
Assert: emptyStringVar = "";
Assert: emptyStringVar = <>;
Assert: emptyStringVar is empty;
Assert: |emptyStringVar| = 0;</langsyntaxhighlight>
 
Checking that a string is _not_ empty generally isn't any more interesting, just a logical negation of the above tests.
 
<langsyntaxhighlight Availlang="avail">nonemptyStringVar : string := "content!";
Assert: nonemptyStringVar ≠ "";
Assert: nonemptyStringVar ≠ <>;
Assert: ¬nonemptyStringVar is empty;
Assert: |nonemptyStringVar| > 0;</langsyntaxhighlight>
 
The library also defines a type _nonempty string_, which can be leveraged for a type-membership check.
 
<syntaxhighlight lang Avail="avail">Assert: nonemptyStringVar ∈ nonempty string;</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">#!/usr/bin/awk -f
BEGIN {
# Demonstrate how to assign an empty string to a variable.
Line 470:
print "Is b empty ?",length(b)==0;
print "Is b not empty ?",length(b)!=0;
}</langsyntaxhighlight>
{{out}}
<pre>$ awk -f R/tmp/string.awk
Line 484:
 
=={{header|Axe}}==
<langsyntaxhighlight lang="axe">""→Str1
!If length(Str1)
Disp "EMPTY",i
Else
Disp "NOT EMPTY",i
End</langsyntaxhighlight>
 
=={{header|BASIC}}==
 
<langsyntaxhighlight lang="basic">10 LET A$=""
20 IF A$="" THEN PRINT "THE STRING IS EMPTY"
30 IF A$<>"" THEN PRINT "THE STRING IS NOT EMPTY"
40 END</langsyntaxhighlight>
 
==={{header|Applesoft BASIC}}===
The terminating quote may be left off. By default, strings are initially empty so the assignment is not necessary. Another way to check for an empty string is to use the LEN function.
<langsyntaxhighlight lang="basic"> 10 LET A$ = "
40 IF LEN (A$) = 0 THEN PRINT "THE STRING IS EMPTY"
50 IF LEN (A$) THEN PRINT "THE STRING IS NOT EMPTY"</langsyntaxhighlight>
 
==={{header|BaCon}}===
The literal empty string in BaCon is <tt>""</tt>.
 
<langsyntaxhighlight lang="freebasic">' Empty string
a$ = ""
IF a$ = "" THEN PRINT "Empty string"
IF a$ != "" THEN PRINT "Non empty string"</langsyntaxhighlight>
 
There are other ways, such as a zero return from the <tt>LEN(s$)</tt> or <tt>ULEN(utf$)</tt> functions. <tt>EQUAL(s$, "")</tt> would be another way.
 
==={{header|BASIC256}}===
<langsyntaxhighlight lang="basic256">subroutine IsEmpty (s$)
if length(s$) = 0 then
print "String is empty"
Line 529:
u$ = "not empty"
call IsEmpty (u$)
end</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">10 LET A$=""
20 IF A$="" THEN PRINT "The string is empty."
30 IF A$<>"" THEN PRINT "The string is not empty."</langsyntaxhighlight>
 
==={{header|QB64}}===
<langsyntaxhighlight QB64lang="qb64">a$ = ""
If a$ = "" Then Print "Empty String"
If a$ <> "" Then Print a$ 'String is not empty, so print its contents.</langsyntaxhighlight>
 
==={{header|QBasic}}===
<langsyntaxhighlight QBasiclang="qbasic">SUB IsEmpty (s AS STRING)
IF LEN(s) = 0 THEN
PRINT "String is empty"
Line 557:
IsEmpty (t$)
u$ = "not empty"
IsEmpty (u$)</langsyntaxhighlight>
 
==={{header|True BASIC}}===
<langsyntaxhighlight lang="qbasic">SUB IsEmpty(s$)
IF Len(s$) = 0 THEN
PRINT "String is empty"
Line 574:
LET u$ = "not empty"
CALL IsEmpty(u$)
END</langsyntaxhighlight>
 
==={{header|uBasic/4tH}}===
<syntaxhighlight lang="text">t := ""
Print Show(FUNC(_IsEmpty(t)))
u := "not empty"
Line 583:
End
 
_IsEmpty Param (1) : Return (Join ("String is ", Iif(Len(a@), "not ", ""), "empty"))</langsyntaxhighlight>
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">sub IsEmpty (s$)
if len(s$) = 0 then
print "String is empty"
Line 599:
u$ = "not empty"
IsEmpty (u$)
end</langsyntaxhighlight>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">
@echo off
 
Line 617:
::Alternatively,
if %var%@ neq @ echo Var is not a blank string.
</syntaxhighlight>
</lang>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> REM assign an empty string to a variable:
var$ = ""
Line 628:
REM Check that a string is not empty:
IF var$ <> "" THEN PRINT "String is not empty"
</syntaxhighlight>
</lang>
 
=={{header|BQN}}==
Line 635:
To check emptiness, we can check the length of the array using <code>≠</code> (or shape with <code>≢</code>).
 
<langsyntaxhighlight lang="bqn">•Show ""
•Show 0 = ≠ ""
•Show 0 ≠ ≠ ""
•Show "" ≡ ⟨⟩</langsyntaxhighlight><syntaxhighlight lang ="bqn">
⟨⟩
1
0
1</langsyntaxhighlight>
 
=={{header|Bracmat}}==
There are two ways to assign a string to a variable. The variant using the <code>=</code> operator does not evaluate the value before the assignment, the variant using the <code>:</code> (match) operator does. If the value is a string, there is no difference, as a string always evaluates to itself.
<langsyntaxhighlight lang="bracmat">( :?a
& (b=)
& abra:?c
Line 655:
& !d:~ { neither is d an empty string }
)
</syntaxhighlight>
</lang>
 
=={{header|Burlesque}}==
Line 661:
Empty string is <tt>""</tt> and checking for empty strings (or empty lists) can be done with the <tt>nu</tt> command.
 
<langsyntaxhighlight lang="blsq">
blsq ) ""
""
Line 668:
blsq ) "a"nu
0
</syntaxhighlight>
</lang>
 
=={{header|C}}==
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.
<langsyntaxhighlight Clang="c">#include <string.h>
 
/* ... */
Line 691:
/* or compare to a known empty string, same thing. "== 0" means strings are equal */
if (strcmp(str, "") == 0) { ... }
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
class Program {
Line 702:
if (!string.IsNullOrEmpty(example)) { }
}
}</langsyntaxhighlight>
 
===In depth===
Line 709:
Note: implementation information provided in comments was obtained reflecting .NET libraries and viewing the .NET Core reference source and may not be correct or remain relevant as time passes.
 
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 821:
P(!EqualityComparer<string>.Default.Equals(s, ""));
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <string>
 
// ...
Line 862:
// create empty string as default parameter
void func( std::string& s = {} ); // {} generated default std:string instance
</syntaxhighlight>
</lang>
 
=={{header|Caché ObjectScript}}==
<langsyntaxhighlight Cachélang="caché ObjectScriptobjectscript">EMPTYSTR
; Demonstrate how to assign an empty string to a variable.
set x = ""
Line 879:
write !,"String length: "_$length(x)_", Equals null: "_(x = "")_", Empty pattern: "_(x?."")
quit</langsyntaxhighlight>
 
{{out}}SAMPLES>do EMPTYSTR^ROSETTA
Line 890:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">
(def x "") ;x is "globally" declared to be the empty string
(let [x ""]
Line 897:
(= x "") ;true if x is the empty string
(not= x "") ;true if x is not the empty string
</syntaxhighlight>
</lang>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol">
IDENTIFICATION DIVISION.
PROGRAM-ID. EMPTYSTR.
Line 924:
 
STOP RUN.
</syntaxhighlight>
</lang>
 
=={{header|CoffeeScript}}==
Empty strings are mostly straightforward in CoffeeScript, but there's one gotcha.
 
<langsyntaxhighlight lang="coffeescript">
isEmptyString = (s) ->
# Returns true iff s is an empty string.
Line 942:
console.log (s = '') == "" # true
console.log new String() == '' # false, due to underlying JavaScript's distinction between objects and primitives
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
Common Lisp treats empty strings as true (T in Common Lisp), therefore one must check the length of the string to know if it is empty or not.
<langsyntaxhighlight lang="lisp">
(defparameter *s* "") ;; Binds dynamic variable *S* to the empty string ""
(let ((s "")) ;; Binds the lexical variable S to the empty string ""
Line 956:
(typep s '(and string
(not (string 0))))) ;; only returns true on string that is not empty
</syntaxhighlight>
</lang>
 
=={{header|Component Pascal}}==
BlackBox Component Builder
<langsyntaxhighlight lang="oberon2">
MODULE EmptyString;
IMPORT StdLog;
Line 980:
END Do;
END EmptyString.
</syntaxhighlight>
</lang>
Execute: ^Q EmptyString.Do<br/>
{{out}}
Line 993:
=={{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 emptiness.
<langsyntaxhighlight lang="d">import std.array;
 
bool isEmptyNotNull(in string s) pure nothrow @safe {
Line 1,019:
assert(s1.empty);
assert(s2.isEmptyNotNull());
}</langsyntaxhighlight>
 
=={{header|Dart}}==
 
<langsyntaxhighlight lang="dart">main() {
var empty = '';
 
Line 1,033:
print('it is not empty');
}
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">program EmptyString;
 
{$APPTYPE CONSOLE}
Line 1,055:
s := 'abc';
Writeln(StringIsEmpty(s)); // False
end.</langsyntaxhighlight>
 
=={{header|Diego}}==
Assign an empty string to variable <code>s</code>.
<langsyntaxhighlight lang="diego">add_str(s,⟦⟧); // empty string
add_str(n); // null string (␀)</langsyntaxhighlight>
 
Check a string is empty (various approaches).
<langsyntaxhighlight lang="diego">add_str(es,⟦⟧);
add_bool(isEmpty)
()_check⟦[es]==⟦⟧⟧;
Line 1,079:
;
log_console()_(isEmpty);
log_console()_is(es); // is not null</langsyntaxhighlight>
 
{{out}}
Line 1,086:
 
Check a string is not empty (various approaches).
<langsyntaxhighlight lang="diego">add_str(s,⟦text⟧);
add_bool(isNotEmpty)
()_check⟦[s]!=⟦⟧⟧;
Line 1,101:
;
log_console()_(isNotEmpty);
log_console()_is(s); // is not null</langsyntaxhighlight>
 
{{out}}
Line 1,108:
 
Check a string is <code>null</code> (␀) (various approaches).
<langsyntaxhighlight lang="diego">add_str(n);
add_bool(isNull)
()_check⟦![n]⟧;
Line 1,119:
(n)_equal⟦␀⟧;
;
log_console()_(isNull);</langsyntaxhighlight>
 
{{out}}
Line 1,125:
 
=={{header|DWScript}}==
<langsyntaxhighlight lang="delphi">var s : String;
 
s := ''; // assign an empty string (can also use "")
Line 1,135:
 
if s <> '' then
PrintLn('not empty');</langsyntaxhighlight>
 
=={{header|Dyalect}}==
Line 1,141:
Demonstrate how to assign an empty string to a variable:
 
<langsyntaxhighlight lang="dyalect">var str = ""</langsyntaxhighlight>
 
Demonstrate how to check that a string is empty:
 
<langsyntaxhighlight lang="dyalect">if str.IsEmpty() { }</langsyntaxhighlight>
 
Demonstrate how to check that a string is not empty:
 
<langsyntaxhighlight lang="dyalect">if !str.IsEmpty() { }</langsyntaxhighlight>
 
=={{header|Déjà Vu}}==
Like in Python, empty strings are falsy, non-empty strings are truthy.
<langsyntaxhighlight lang="dejavu">local :e ""
 
if not e:
Line 1,159:
 
if e:
!print "not an empty string"</langsyntaxhighlight>
 
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">a$ = ""
if a$ = ""
print "empty"
Line 1,169:
if a$ <> ""
print "no empty"
.</langsyntaxhighlight>
 
=={{header|Elena}}==
ELENA 4.x:
<langsyntaxhighlight lang="elena">import extensions;
public program()
Line 1,184:
if (s.isNonempty())
{ console.printLine("'", s, "' is not empty") }
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,192:
=={{header|Elixir}}==
To check whether a given variable holds an empty string, either compare it to the empty string literal, check its length - O(M), or check it's byte size - O(1)).
<langsyntaxhighlight lang="elixir">
empty_string = ""
not_empty_string = "a"
Line 1,209:
byte_size(not_empty_string) == 0
# => false
</syntaxhighlight>
</lang>
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight Lisplang="lisp">(setq str "") ;; empty string literal
 
(if (= 0 (length str))
(message "string is empty"))
(if (/= 0 (length str))
(message "string is not empty"))</langsyntaxhighlight>
 
Also possible is <code>(string= "" str)</code>.
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">
1> S = "". % erlang strings are actually lists, so the empty string is the same as the empty list [].
[]
Line 1,231:
4> case "aoeu" of [] -> empty; [H|T] -> not_empty end.
not_empty
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">sequence s
 
-- assign an empty string
Line 1,248:
if length(s) then
-- string is not empty
end if</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System
 
[<EntryPoint>]
Line 1,258:
printfn "Is empty %A: %A" emptyString (emptyString = String.Empty)
printfn "Is not empty %A: %A" emptyString (emptyString <> String.Empty)
0</langsyntaxhighlight>
{{out}}
<pre>Is empty "": true
Line 1,267:
It's idiomatic in Factor to prefer using the stack over variable bindings.
 
<langsyntaxhighlight lang="factor">"" empty? .</langsyntaxhighlight>
{{out}}
<pre>
Line 1,275:
However, Factor provides lexical variables:
 
<langsyntaxhighlight lang="factor">USE: locals
[let
"" :> empty-string
empty-string empty? .
empty-string empty? not .
]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,291:
Fantom uses "" to represent an empty string, and provides the isEmpty method to check if a string is empty.
 
<langsyntaxhighlight lang="fantom">
a := "" // assign an empty string to 'a'
a.isEmpty // method on sys::Str to check if string is empty
Line 1,297:
a == "" // alternate check for an empty string
!a.isEmpty // check that a string is not empty
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
Strings are represented as an addr-len pair on the stack. An empty string has len 0.
<langsyntaxhighlight lang="forth">
\ string words operate on the address and count left on the stack by a string
\ ? means the word returns a true/false flag on the stack
Line 1,308:
: filled? ( c-addr u -- ? ) empty? 0= ;
: ="" ( c-addr u -- ) drop 0 ; \ It's OK to copy syntax from other languages
</syntaxhighlight>
</lang>
 
Forth Console Test (True= -1, False=0)
Line 1,324:
 
Such variables, or text literals, may be passed as a parameter to a subprogram, and it may use the LEN function to ascertain the size of the parameter, which in that sense could be considered a string because CHARACTER parameters are passed with a secret additional parameter, their size, which is available to the LEN function within the subprogram.
<langsyntaxhighlight lang="fortran"> SUBROUTINE TASTE(T)
CHARACTER*(*) T !This form allows for any size.
IF (LEN(T).LE.0) WRITE(6,*) "Empty!"
Line 1,334:
TEXT = "" !Fills the entire variable with space characters.
CALL TASTE(TEXT) !Passes all 24 of them. Result is Not empty!
END</langsyntaxhighlight>
Otherwise, you could employ the Fortran protocol that trailing spaces are irrelevant in text comparisons. Thus <code>TEXT .EQ. ""</code> would give ''true'' even though TEXT might contain thousands of space characters, and so would <code>TEXT .EQ. " "</code> - thus an empty string is one containing nothing other than spaces.
 
Line 1,343:
=={{header|Free Pascal}}==
Assigning an empty string:
<langsyntaxhighlight lang="pascal">s := '';</langsyntaxhighlight>
Checking for an empty string:
<langsyntaxhighlight lang="pascal">s = ''
length(s) = 0</langsyntaxhighlight>
Checking for a non-empty string:
<langsyntaxhighlight lang="pascal">s <> ''
length(s) > 0
longBool(length(s))</langsyntaxhighlight>
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}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Sub IsEmpty(s As String)
Line 1,370:
Dim u As String = "not empty"
IsEmpty(u)
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,380:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">a = ""
if a == ""
println["empty"]
 
if a != ""
println["Not empty"]</langsyntaxhighlight>
 
=={{header|FutureBasic}}==
<langsyntaxhighlight lang="futurebasic">window 1, @"Empty string", (0,0,480,270)
 
CFStringRef s
Line 1,404:
if ( len(s) != 0 ) then print @"string not empty"
 
HandleEvents</langsyntaxhighlight>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=4d8a208f49364dc25361bf2042af1005 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim sString As String[] = ["", "Hello", "world", "", "Today", "Tomorrow", "", "", "End!"]
Dim sTemp As String
Line 1,422:
Next
 
End</langsyntaxhighlight>
Output:
<pre>
Line 1,440:
In Go variables are always initialized to a provided value or to the "zero value" of the type.
The zero value of a string is the empty string.
<langsyntaxhighlight lang="go">// define and initialize an empty string
var s string
s2 := ""
Line 1,453:
// check that a string is not empty, any of:
s != ""
len(s) != 0 // or > 0</langsyntaxhighlight>
::<langsyntaxhighlight lang="go">package main
 
import (
Line 1,476:
// check that a string is not empty.
test(str2) // prt not empty
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def s = '' // or "" if you wish
assert s.empty
 
s = '1 is the loneliest number'
assert !s.empty</langsyntaxhighlight>
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">10 DIM S1$ 'implicitly defined empty string
20 S2$ = "" 'explicitly defined empty string
30 S3$ = "Foo bar baz"
Line 1,494:
70 END
200 IF LEN(S$)=0 THEN PRINT "Empty string" ELSE PRINT "Non-empty string"
210 RETURN</langsyntaxhighlight>
 
=={{header|Harbour}}==
<langsyntaxhighlight lang="visualfoxpro">// in Harbour we have several functions to check emptiness of a string, f.e. hb_IsNull(), Len(), Empty() et.c.,
// we can also use comparison expressions like [cString == ""] and [cString != ""], yet the most convenient
// of them is `Empty()` (but that depends on personal coding style).
Line 1,504:
IF ! Empty( cString ) // --> FALSE
? cString
ENDIF</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad
 
-- In Haskell strings are just lists (of characters), so we can use the function
Line 1,516:
let s = ""
when (null s) (putStrLn "Empty.")
when (not $ null s) (putStrLn "Not empty.")</langsyntaxhighlight>
 
=={{header|HolyC}}==
<langsyntaxhighlight lang="holyc">/* assign an empty string */
U8 *str = StrNew("");
/* or */
Line 1,530:
 
/* to test if string is not empty */
if (StrLen(str)) { ... }</langsyntaxhighlight>
 
=={{header|i}}==
<langsyntaxhighlight lang="i">software {
s = ""
Line 1,546:
end
}
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Icon and Unicon can produce empty strings in several ways:
<langsyntaxhighlight Iconlang="icon">s := "" # null string
s := string('A'--'A') # ... converted from cset difference
s := char(0)[0:0] # ... by slicing
Line 1,565:
s := &null # NOT a null string, null type
/s # test for null type
\s # test for non-null type </langsyntaxhighlight>
 
=={{header|J}}==
 
<langsyntaxhighlight lang="j"> variable=: ''
0=#variable
1
0<#variable
0</langsyntaxhighlight>
 
Note that J attempts to make no distinction between empty lists, regardless of their type. In other words, while some operations can reveal the type of an empty list (for example, anything that can introduce padding based on the type of the list itself) this distinction is ignored whenever possible. You can perform arithmetic on an empty string, and you can append text to an empty list of numbers even though these operations would not succeed on non-empty lists of the same type.
Line 1,584:
example:
 
<langsyntaxhighlight Jlang="j"> ''
 
EMPTY
Line 1,590:
0
$EMPTY
0 0</langsyntaxhighlight>
 
Here, the display of an empty string occupies one line (just like the display of any other string -- except this line is an empty line), while the display of <code>EMPTY</code> occupies zero blank lines. Or <code>EMPTY</code> contains zero empty strings (or zero empty lists). When you want the result of a J verb to not display any result, use a value like EMPTY as its explicit result.
Line 1,596:
=={{header|Java}}==
<code>String.isEmpty()</code> is part of Java 1.6. Other options for previous versions are noted.
<langsyntaxhighlight 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");
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Create an empty String
<langsyntaxhighlight lang="javascript">var s = "";
var s = new String();</langsyntaxhighlight>
 
Boolean expressions representing emptiness
<langsyntaxhighlight lang="javascript">s == ""
s.length == 0
!s
!Boolean(s)</langsyntaxhighlight>
 
Non-emptiness
<langsyntaxhighlight lang="javascript">!!s
s != ""
s.length != 0
s.length > 0
Boolean(s)</langsyntaxhighlight>
 
=={{header|jq}}==
jq strings are JSON strings. The empty string literal is simply <tt>""</tt>. It can be assigned to a variable as illustrated by this example:<langsyntaxhighlight lang="jq">"" as $x </langsyntaxhighlight>If s is a string or an array, then the additive "zero" for s can be created by writing s[0:0]. That is, if s is a string, then s[0:0] will yield the empty string. This is useful when writing polymorphic functions.
 
To determine whether a string, s, is empty:<langsyntaxhighlight lang="jq">s == ""
# or:
s|length == 0</langsyntaxhighlight>To determine whether a string, s, is non-empty:<langsyntaxhighlight lang="jq">s != ""
# or:
s.length != 0 # etc.</langsyntaxhighlight>
 
=={{header|Jsish}}==
<langsyntaxhighlight lang="javascript">/* Empty string, in Jsish */
var em1 = '';
var em2 = new String();
Line 1,679:
(em1 === em2) ==> true
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 1,706:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
blank = ""
nonblank = "!"
Line 1,718:
println("That nonblank is empty is ", isempty(nonblank))
println("That nonblank is not empty is ", !isempty(nonblank))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,734:
{{trans|J}}
 
<langsyntaxhighlight Klang="k"> variable: ""
0=#variable
1
0<#variable
0</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">fun main(args: Array<String>) {
val s = ""
println(s.isEmpty()) // true
Line 1,748:
println(s.none()) // true
println(s.any()) // false
}</langsyntaxhighlight>
 
=={{header|LabVIEW}}==
Line 1,755:
=={{header|Lambdatalk}}==
In Lambdatalk primitives working on sequences of words begin with "S."
<langsyntaxhighlight lang="scheme">
'{def emptyString }
-> emptyString
Line 1,767:
'{= {S.length {emptyString}} 0}
-> true
</syntaxhighlight>
</lang>
 
=={{header|langur}}==
You can use empty quote marks or the ZLS token.
<langsyntaxhighlight lang="langur">val .zls = ZLS
writeln .zls == ""
writeln .zls != ""
writeln len(.zls)</langsyntaxhighlight>
 
{{out}}
Line 1,782:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">//Demonstrate how to assign an empty string to a variable.
local(str = string)
local(str = '')
Line 1,793:
local(str = 'Hello, World!')
#str->size > 0 // true
#str->size // true</langsyntaxhighlight>
 
=={{header|Latitude}}==
 
Assigning the empty string.
<langsyntaxhighlight lang="latitude">s := "".
s := String clone.</langsyntaxhighlight>
 
Checking whether a string is empty.
<langsyntaxhighlight lang="latitude">s == "".
s empty?.
s length == 0.</langsyntaxhighlight>
 
Note that all strings are truthy in Latitude, so simply checking the truthiness of a string is an insufficient means to check whether it is empty.
Line 1,813:
{{trans|Erlang}}
 
<langsyntaxhighlight lang="lisp">
> (set str "")
()
Line 1,838:
> (case "apple" ('() 'empty) ((cons head tail) 'not-empty))
not-empty
</syntaxhighlight>
</lang>
 
=={{header|Lhogho}}==
Lhogho is a Logo compiler for Windows and Linux
<langsyntaxhighlight lang="logo">make "str " ;make null-string word
print empty? :str ;prints 'true'
print not empty? :str ;prints 'false'
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
'assign empty string to variable
a$ = ""
Line 1,857:
if a$<>"" then print "Not empty."
if len(a$)>0 then print "Not empty."
</syntaxhighlight>
</lang>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">str = EMPTY -- same as: str = ""
put str=EMPTY
-- 1
put str<>EMPTY
-- 0</langsyntaxhighlight>
 
=={{header|LOLCODE}}==
The empty string is a false value in LOLCODE, and is thus amenable to use as the condition of an <tt>O RLY?</tt>
<langsyntaxhighlight LOLCODElang="lolcode">HAI 1.3
 
I HAS A string ITZ ""
Line 1,876:
OIC
 
KTHXBYE</langsyntaxhighlight>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
<lang Lua>
-- create an empty string 3 different ways
str = ""
Line 1,906:
end
 
</syntaxhighlight>
</lang>
 
=={{header|M2000 Interpreter}}==
Easy reply for this task
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
A$=""
Print A$<>"", A$="", Len(A$)=0
</syntaxhighlight>
</lang>
 
Depends of variable visibility, and what we want to do: To make a new local, to shadow a local or a global on.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
\\
Line 2,004:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">
<lang Maple>
s := ""; # Create an empty string
evalb(s = ""); # test if the string is empty
evalb(s <> ""); # test if the string is not empty
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">
<lang Mathematica>
str=""; (*Create*)
str==="" (*test empty*)
str=!="" (*test not empty*)
</syntaxhighlight>
</lang>
 
=={{header|MATLAB}} / {{header|Octave}}==
 
<langsyntaxhighlight Matlablang="matlab"> % Demonstrate how to assign an empty string to a variable.
str = '';
% Demonstrate how to check that a string is empty.
Line 2,029:
% Demonstrate how to check that a string is not empty.
~isempty(str)
(length(str)>0)</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">s: ""$
 
/* check using string contents */
Line 2,040:
/* check using string length */
slength(s) = "";
slength(s) # "";</langsyntaxhighlight>
 
=={{header|Mercury}}==
Line 2,046:
There's nothing special about empty strings in Mercury.
 
<langsyntaxhighlight Mercurylang="mercury">S = "", % assignment
 
( if S = "" then ... else ... ), % checking if a string is empty
 
( if not S = "" then ... else ... ), % checking if a string is not empty</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.3}}
The <code>bool</code> operator returns <code>false</code> on an empty string and <code>true</code> on a non-empty string. We can define <code>empty?</code> as the negation of <code>bool</code>.
<langsyntaxhighlight lang="min">(bool not) :empty?
"" empty? puts!
"Rosetta Code" empty? puts!</langsyntaxhighlight>
{{out}}
<pre>
Line 2,068:
 
Storing an empty string:
<langsyntaxhighlight lang="mips">li t0,0
la a0,UserRam
sb t0,(a0)</langsyntaxhighlight>
 
Checking that a string is (not) empty:
 
<langsyntaxhighlight lang="mips">lbu t0,(a0)
nop ;load delay slot (only necessary on MIPS I hardware)
beqz t0,StringIsEmpty
nop ;branch delay slot
 
;your code for what happens when the string is not empty, goes here</langsyntaxhighlight>
 
=={{header|Mirah}}==
<langsyntaxhighlight lang="mirah">empty_string1 = ""
empty_string2 = String.new
 
puts "empty string is empty" if empty_string1.isEmpty()
puts "empty string has no length" if empty_string2.length() == 0
puts "empty string is not nil" unless empty_string1 == nil</langsyntaxhighlight>
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">MODULE EmptyString EXPORTS Main;
 
IMPORT IO,Text;
Line 2,109:
(* To check for a non-empty string, negate any of the above
conditions with NOT *)
END EmptyString.</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight lang="nanoquery">s = ""
 
if len(s)=0
Line 2,118:
else
println "s is not empty"
end</langsyntaxhighlight>
 
=={{header|Nemerle}}==
Assign an empty string:
<langsyntaxhighlight Nemerlelang="nemerle">def empty = "";
mutable fill_later = "";</langsyntaxhighlight>
Check if a string is empty/not empty:
<langsyntaxhighlight Nemerlelang="nemerle">a_string == ""; a_string != 0;
a_string.Length == 0; a_string.Length > 0;</langsyntaxhighlight>
 
=={{header|NESL}}==
<langsyntaxhighlight lang="nesl">my_empty_string = "";
 
% To make sure it is empty, we can ask whether its length is equal to zero. %
 
#my_empty_string == 0;</langsyntaxhighlight>
{{out}}
<pre>my_empty_string = "" : [char]
Line 2,140:
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols binary
 
Line 2,161:
 
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,171:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">var x = ""
 
if x == "":
Line 2,182:
echo "empty"
if x.len > 0:
echo "not empty"</langsyntaxhighlight>
 
=={{header|NS-HUBASIC}}==
<langsyntaxhighlight NSlang="ns-HUBASIChubasic">10 S$=""
20 IF S$<>"" THEN O$="NOT "
30 PRINT "THE STRING IS "O$"EMPTY."</langsyntaxhighlight>
 
=={{header|Nyquist}}==
===Lisp Syntax===
<langsyntaxhighlight lang="lisp">
(setf emptystring "") ;binds variable'emptystring' to the empty string ""
 
Line 2,205:
(when (and (stringp emptystring)(> (length emptystring) 0))
(print "Is a non-empty string")))
</syntaxhighlight>
</lang>
 
===SAL Syntax===
<langsyntaxhighlight lang="sal">
define variable emptystring = "" ;binds variable'emptystring' to the empty string ""
 
Line 2,215:
else
print "is not empty string"
</syntaxhighlight>
</lang>
 
 
===Audacity plug-in (LISP syntax)===
<langsyntaxhighlight lang="lisp">
;nyquist plug-in
;version 4
Line 2,230:
"The string is empty."
"The string is not empty.")
</syntaxhighlight>
</lang>
 
 
===Audacity plug-in (SAL syntax)===
<syntaxhighlight lang="nyquist">
<lang Nyquist>
;nyquist plug-in
;version 4
Line 2,247:
"The string is empty.",
"The string is not empty.")
</syntaxhighlight>
</lang>
 
=={{header|oberon-2}}==
{{works with|oo2c version 2}}
<langsyntaxhighlight lang="oberon2">
MODULE EmptyString;
IMPORT Out;
Line 2,266:
Out.String("checking str[0] = 0X. Is Empty? ");Out.Bool(str[0] = 0X);Out.Ln;
END EmptyString.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,278:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
s := "";
if(s->IsEmpty()) {
Line 2,285:
"s is not empty"->PrintLine();
};
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let is_string_empty s =
(s = "")
 
Line 2,297:
Printf.printf "s1 empty? %B\n" (is_string_empty s1);
Printf.printf "s2 empty? %B\n" (is_string_empty s2);
;;</langsyntaxhighlight>
 
{{out}}
Line 2,309:
isEmpty can be used on all collections, not only strings.
 
<langsyntaxhighlight Oforthlang="oforth">"" isEmpty
"" isEmpty not</langsyntaxhighlight>
 
=={{header|Ol}}==
 
<syntaxhighlight lang="ol">
<lang ol>
; define the empty string
(define empty-string "")
Line 2,332:
(less? 0 (string-length the-string)))
(print "the-string is NOT empty))
</syntaxhighlight>
</lang>
 
=={{header|ooRexx}}==
should work with each and every REXX interpreter/compiler.
<langsyntaxhighlight lang="oorexx">v=''
w=' '
if v=='' Then Say 'v contains the empty string'<
If length(w)>0 Then Say 'Variable w does not contain the empty string'
If w='' Then Say 'this is not a good test' </langsyntaxhighlight>
{{out}}
<pre>v contains the empty string
Line 2,349:
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.
 
<langsyntaxhighlight lang="progress">DEFINE VARIABLE cc AS CHARACTER.
 
IF cc > '' THEN
Line 2,357:
ELSE /* IF cc = '' */
MESSAGE 'empty' VIEW-AS ALERT-BOX.
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">a="";
isEmpty(s)=s=="" \\ Alternately:
isEmpty(s)=#s==0
isNonempty(s)=s!="" \\ Alternatively:
isNonempty(s)=#s</langsyntaxhighlight>
 
=={{header|Pascal}}==
''See also [[#Delphi|Delphi]] or [[#Free Pascal|Free Pascal]]''
{{works with|Extended Pascal}}
<langsyntaxhighlight lang="pascal">program emptyString(output);
var
s: string(20);
Line 2,392:
{ you will need to call `trim` (remove trailing blanks) first. }
writeLn('trim(s) = '''' :':20, trim(s) = '':6)
end.</langsyntaxhighlight>
{{out}}
EQ(s, '&#39;) : True
Line 2,402:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">if ($s eq "") { # Test for empty string
print "The string is empty";
}
if ($s ne "") { # Test for non empty string
print "The string is not empty";
}</langsyntaxhighlight>
 
In Perl, an empty string is often used to represent a false value.
<langsyntaxhighlight Perllang="perl">$s = "";
if ($s) { ... } # false
 
Line 2,423:
# but a string that converts to number 0 is not always false, though:
$s = "0 but true";
if ($s) { ... } # it's true! black magic!</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span>
Line 2,438:
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- string is not empty</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- string is not empty</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PHP}}==
<langsyntaxhighlight Phplang="php"><?php
 
$str = ''; // assign an empty string to a variable
Line 2,456:
 
if (strlen($str) == 0) { ... }
if (strlen($str) != 0) { ... }</langsyntaxhighlight>
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">main =>
S = "", % assign an empty string to a variable
S == "", % check that a string is empty,
"not empty" != "". % check that a string is not empty</langsyntaxhighlight>
 
In Picat a string is a list of characters so list notation can be also be used.
<langsyntaxhighlight Picatlang="picat">main =>
S = "", % assign an empty string to a variable
S == [], % check that a string is empty,
"not empty" != []. % check that a string is not empty</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
The empty string is represented by '[http://software-lab.de/doc/ref.html#nilSym NIL]' in PicoLisp. During input, two subsequent double quotes '""' return the symbol NIL.
<langsyntaxhighlight PicoLisplang="picolisp"># To assign a variable an empty string:
(off String)
(setq String "")
Line 2,485:
(and String ..)
(if String ..)
(when String ..)</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight Pikelang="pike">int main() {
string s;
 
Line 2,499:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">Dcl s Char(10) Varying;
s = ''; /* assign an empty string to a variable. */
if length(s) = 0 then ... /* To test whether a string is empty */
if length(s) > 0 then ... /* to test for a non-empty string */
</syntaxhighlight>
</lang>
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Put "" into a string.
Line 2,515:
If the string is not blank, write "Not empty!" on the console.
Wait for the escape key.
Shut down.</langsyntaxhighlight>
 
=={{header|PowerShell}}==
Assignment (the type identifier is optional):
<syntaxhighlight lang="powershell">
<lang PowerShell>
[string]$alpha = "abcdefghijklmnopqrstuvwxyz"
[string]$empty = ""
# or...
[string]$empty = [String]::Empty
</syntaxhighlight>
</lang>
Tests:
<syntaxhighlight lang="powershell">
<lang PowerShell>
[String]::IsNullOrEmpty($alpha)
[String]::IsNullOrEmpty($empty)
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,538:
=={{header|Prolog}}==
{{works with|SWI-Prolog|7}}
<langsyntaxhighlight lang="prolog">assign_empty_string(Variable) :- Variable = "".
 
is_empty_string(String) :- String == "".
not_empty_string(String) :- String \== "".
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
In PureBasic we can just test a string for truth to determine if it has a value.
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.s isStringEmpty(a.s)
If a
ProcedureReturn "String is not empty, it contains '" + a + "'."
Line 2,562:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
{{out}}
<pre>String is empty, or null.
Line 2,571:
Any non-empty ''string'', including '0', is always treated as True in a boolean context.
 
<langsyntaxhighlight lang="python">
s = ''
# or:
Line 2,598:
elif s is None:
return True
</syntaxhighlight>
</lang>
 
=={{header|QB64}}==
<langsyntaxhighlight lang="qbasic">Dim s1 As String 'initialized empty
If Len(s1) = 0 Then Print "Empty"
 
Line 2,609:
s3$ = "cat"
If Len(s3$) <> 0 Then Print "Not empty"
If s3$ <> "" Then Print "Still not empty"</langsyntaxhighlight>
{{out}}
<pre>
Line 2,623:
Demonstrated as a dialogue in the Quackery REPL.
 
<langsyntaxhighlight Quackerylang="quackery">Welcome to Quackery.
 
Enter "leave" to leave the shell.
Line 2,641:
...
 
Aloha.</langsyntaxhighlight>
 
=={{header|Quite BASIC}}==
<langsyntaxhighlight Quitelang="quite BASICbasic">10 let s=""
20 if s="" then let o=""
30 if s<>"" then let o="not "
40 print "The string is ";o;"empty."</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">s <- ''
 
if (s == '') cat('Empty\n')
Line 2,658:
if (s != '') cat('Not empty\n')
#or
if (nchar(s) > 0) cat('Not empty\n')</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(define empty-string "")
(define (string-null? s) (string=? "" s))
(define (string-not-null? s) (string<? "" s))</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" perl6line>my $s = '';
say 'String is empty' unless $s;
say 'String is not empty' if $s;</langsyntaxhighlight>
 
Unlike in Perl 5, only empty strings test as false - the string "0" tests as true now.
 
=={{header|Rascal}}==
<langsyntaxhighlight lang="rascal">str s = "";
if (s=="") print("string s is empty");
if (s!="") print("string s is not empty");
</syntaxhighlight>
</lang>
Or the build-in operator:
<langsyntaxhighlight lang="rascal">import String;
if (isEmpty(s)) print("string s is empty");
if (isEmpty(s)) print("string s is not empty");</langsyntaxhighlight>
 
=={{header|Red}}==
<langsyntaxhighlight Redlang="red">Red []
s: copy "" ;; assign empty string
?? s
Line 2,694:
s: "abc"
prin s unless empty? s [print " not empty"]
</syntaxhighlight>
</lang>
{{out}}
<pre>s: ""
Line 2,704:
=={{header|Retro}}==
Create an empty string and assign it to a variable. In these '''keepString''' is used to ensure that the string is permanent.
<syntaxhighlight lang="retro">
<lang Retro>
( by creating a variable )
"" keepString variable: foo
Line 2,710:
( by setting an existing variable 'foo' )
"" keepString !foo
</syntaxhighlight>
</lang>
 
Checking that a string is empty. A string with a length of zero is assumed to be empty.
 
<syntaxhighlight lang="retro">
<lang Retro>
: emtpy? ( $-f ) getLength 0 = ;
 
"" empty? putn
"hello" empty? putn
</syntaxhighlight>
</lang>
 
Check that a string is not empty.
 
<syntaxhighlight lang="retro">
<lang Retro>
: notEmpty? ( $-f ) getLength 0 > ;
 
"" notEmpty? putn
"hello" notEmpty? putn
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program shows how to assign an empty string, & then check for empty/not-empty str*/
 
/*─────────────── 3 simple ways to assign an empty string to a variable.*/
Line 2,769:
if length(cod)\=0 then fish=cod /*a not-as-fast compare. */
 
/*────────────────────────── anyway, as they say: "choose your poison." */</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
cStr = NULL # empty string
if cStr = NULL
Line 2,779:
see "cstr is not empty string!" + nl
ok
</syntaxhighlight>
</lang>
 
=={{header|Robotic}}==
<langsyntaxhighlight lang="robotic">
set "$string" to ""
if "$string.length" = 0 then "empty"
Line 2,791:
* "Empty string"
end
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
 
Create an empty string
<langsyntaxhighlight lang="ruby">s = ""
s = String.new
s = "any string"; s.clear</langsyntaxhighlight>
 
These expressions all evaluate to true to determine emptiness:
<langsyntaxhighlight lang="ruby">s == ""
s.eql?("")
s.empty?
Line 2,808:
 
# also silly things like
s.each_char.to_a.empty?</langsyntaxhighlight>
 
Non-empty expressions, in addition to simply negating the above expressions:
<langsyntaxhighlight lang="ruby">s != ""
s.length > 0
s[/./m]</langsyntaxhighlight>
 
Note that we can '''not''' do the following, because the empty string is equivalent to true in Ruby ([[Boolean values#Ruby]]):
<langsyntaxhighlight lang="ruby">if s then puts "not empty" end # This code is wrong!</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">var$ = ""
' --------------
'empty string
Line 2,829:
' -------------
if var$<>"" then print "String Not empty."
if len(var$)>0 then print "String Not empty."</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">let s = "";
println!("is empty: {}", s.is_empty());
let t = "x";
Line 2,840:
let b = "x".to_string();
println!("is empty: {}", b.is_empty());
println!("is not empty: {}", !b.is_empty());</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">// assign empty string to a variable
val s=""
// check that string is empty
Line 2,852:
s.nonEmpty // false
s!="" // false
s.size>0 // false</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define empty-string "")
(define (string-null? s) (string=? "" s))
(define (string-not-null? s) (string<? "" s))</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7"># assign empty string to a variable
s := ""
 
Line 2,867:
 
# check that string is not empty
s <> ""</langsyntaxhighlight>
 
=={{header|Self}}==
<langsyntaxhighlight lang="self">
"Put an empty string in a slot called 'str'"
str: ''.
Line 2,878:
"Check that string is not empty"
str isEmpty not.</langsyntaxhighlight>
 
=={{header|SenseTalk}}==
<langsyntaxhighlight lang="self">
set emptyString to ""
 
Line 2,889:
 
put emptyString is not empty
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,898:
=={{header|Sidef}}==
Create an empty string:
<langsyntaxhighlight lang="ruby">var s = "";
var s = String.new;</langsyntaxhighlight>
 
These expressions all evaluate to true to determine emptiness:
<langsyntaxhighlight lang="ruby">s == "";
s.length == 0;
s.is_empty;
s ~~ /^\z/;
s ~~ /\A\z/;</langsyntaxhighlight>
 
Non-empty expressions, in addition to simply negating the above expressions:
<langsyntaxhighlight lang="ruby">s != "";
s.length > 0;
s ~~ /./s;
s !~ /^\z/;</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk">"Assign empty string to a variable"
str := ''.
 
Line 2,926:
str notEmpty
str size = 0
str = ''</langsyntaxhighlight>
Notice that assignment is ":=" whereas equality check is "=".
 
=={{header|SNOBOL4}}==
An assignment statement with nothing to the right of the <tt>=</tt> operator assigns the empty string (or, as it is more commonly called in SNOBOL, the null string).
<langsyntaxhighlight lang="snobol4">* ASSIGN THE NULL STRING TO X
X =
* CHECK THAT X IS INDEED NULL
Line 2,938:
YES OUTPUT = 'NULL'
END
</syntaxhighlight>
</lang>
{{out}}
<pre>NULL</pre>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">(* Assign empty string to a variable *)
val s = ""
(* Check that a string is empty*)
s = ""
(* Check that a string is nonempty *)
s <> ""</langsyntaxhighlight>
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">scalar s=""
 
display s==""
 
* Alternatively, check the length
display length(s)==0</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">var s = ""
if s.isEmpty { // alternately, s == ""
println("s is empty")
} else {
println("s is not empty")
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
The only special position that the empty string has in Tcl is that a great many commands return it, and the REPL of [[tclsh]] and [[wish]] doesn't print it. Otherwise, it is just like any other value.
<langsyntaxhighlight lang="tcl">set s ""
if {$s eq ""} {puts "s contains an empty string"}
if {$s ne ""} {puts "s contains a non-empty string"}</langsyntaxhighlight>
There are other ways to check for emptiness and non-emptiness too (though the above are favored for reasons of simplicity, clarity and speed):
<langsyntaxhighlight lang="tcl">if {[string equal $s ""]} {puts "is empty"}
if {[string length $s] == 0} {puts "is empty"}
if {[string compare $s ""] != 0} {puts "is non-empty"}</langsyntaxhighlight>
 
=={{header|TorqueScript}}==
Line 2,985:
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
s=""
IF (s=="") PRINT "s is an empty string"
IF (s!="") PRINT "s is a non-empty string"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,000:
====Pattern Matching====
 
<langsyntaxhighlight lang="txr">@(bind a "")</langsyntaxhighlight>
 
If <code>a</code> is unbound, a binding is created, containing the empty string.
Line 3,008:
====TXR Lisp====
 
<langsyntaxhighlight lang="txrlisp">(defvarl a "")
 
(if (equal a "")
Line 3,016:
 
(if (zerop (length a))
(format t "guess what?\n"))</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
 
<langsyntaxhighlight lang="bash"># assign an empty string to a variable
s=""
 
Line 3,032:
# examine the length of the string
if [ -z "$s" ]; then echo "the string has length zero: it is empty"; fi
if [ -n "$s" ]; then echo "the string has length non-zero: it is not empty"; fi</langsyntaxhighlight>
 
When using comparison operators, it is crucial to double-quote the variable within the conditional expression. This will ensure the shell sees the correct number of arguments. For example, if one were to write <tt>[ $s = "" ]</tt>, then after variable substitition, the shell will try to evaluate <tt>[ = "" ]</tt> which is a syntax error.
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">decl string s
set s ""
 
Line 3,044:
else
out "not empty" endl console
end if</langsyntaxhighlight>
 
=={{header|VAX Assembly}}==
 
<langsyntaxhighlight VAXlang="vax Assemblyassembly">desc: .ascid "not empty" ;descriptor (len+addr) and text
.entry empty, ^m<>
tstw desc ;check length field
Line 3,057:
;... empty
ret
.end empty</langsyntaxhighlight>
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">
<lang vb>
dim s as string
 
Line 3,078:
'or:
if Len(s) > 0 then Debug.Print "not empty."
</syntaxhighlight>
</lang>
 
=={{header|VBScript}}==
Line 3,090:
 
===In brief===
<langsyntaxhighlight lang="vbnet">Dim s As String
 
' Assign empty string:
Line 3,103:
' Check for null or empty (more idiomatic in .NET):
If String.IsNullOrEmpty(s) Then
End If</langsyntaxhighlight>
 
===In depth===
Line 3,110:
In .NET Core, functions defined in <code>Microsoft.VisualBasic</code> are only available for .NET Core 3.0 and above.
 
<langsyntaxhighlight lang="vbnet">Option Strict On
 
Module Program
Line 3,238:
P(Not EqualityComparer(Of String).Default.Equals(s, ""))
End Sub
End Module</langsyntaxhighlight>
 
{{out}}
Line 3,272:
 
=={{header|Vlang}}==
<langsyntaxhighlight lang="vlang">// define and initialize an empty string
mut s := ""
 
Line 3,284:
// check that a string is not empty, any of:
s != ""
s.len() != 0 // or > 0</langsyntaxhighlight>
::<langsyntaxhighlight lang="vlang">fn test(s string) {
if s.len() == 0 {
println("empty")
Line 3,301:
// check that a string is not empty.
test(str2) // prt not empty
}</langsyntaxhighlight>
 
=={{header|Wee Basic}}==
<langsyntaxhighlight Weelang="wee Basicbasic">let string$=""
if string$=""
print 1 "The string is empty."
Line 3,310:
print 1 "The string is not empty."
endif
end</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">var isEmpty = Fn.new { |s| s == "" }
 
var s = ""
var t = "0"
System.print("'s' is empty? %(isEmpty.call(s))")
System.print("'t' is empty? %(isEmpty.call(t))")</langsyntaxhighlight>
 
{{out}}
Line 3,328:
=={{header|X86-64 Assembly}}==
===UASM 2.52===
<langsyntaxhighlight lang="asm">
option casemap:none
 
Line 3,361:
main endp
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,369:
=={{header|XLISP}}==
<code>STRING-NULL?</code> returns true if its argument is a string of length zero. In a REPL:
<langsyntaxhighlight lang="scheme">[1] (define my-empty-string "") ;; assign an empty string to a variable
 
MY-EMPTY-STRING
Line 3,377:
[3] (string-null? "A non-empty string")
 
()</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code Text=12;
string 0; \use zero-terminated convention, instead of MSb set
char S;
Line 3,389:
if S(0) # 0 then Text(0, "not empty
");
]</langsyntaxhighlight>
 
=={{header|Z80 Assembly}}==
{{trans|6502 Assembly}}
An empty string is just a null terminator with no text in front.
<langsyntaxhighlight lang="z80">EmptyString:
byte 0</langsyntaxhighlight>
 
Checking if a string is empty is simple, just count the number of characters before you reach the terminator. If that count equals zero, the string is empty. Otherwise, the string is not empty.
 
<langsyntaxhighlight lang="z80">ld hl,MyString
 
GetStringLength:
Line 3,415:
or a ;compare A to zero (effectively comparing B to zero)
jr z,StringIsEmpty
;your code for what happens when MyString is not empty goes here.</langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">s:=""; // or s:=String, String is the object ""
s.toBool() //-->False
if (s) println("not empty")</langsyntaxhighlight>
 
=={{header|Zoomscript}}==
For typing:
<langsyntaxhighlight Zoomscriptlang="zoomscript">var string
string = ""
if eq string ""
Line 3,430:
else
print "The string is not empty."
endif</langsyntaxhighlight>
For importing:
 
Line 3,439:
 
=={{header|Zig}}==
<langsyntaxhighlight lang="zig">const std = @import("std");
 
pub fn main() !void {
Line 3,450:
std.debug.print("string empty\n", .{});
}
}</langsyntaxhighlight>
 
{{omit from|GUISS}}
10,327

edits