Empty string
This article needs additional citations for verification. (November 2009) |
In
Formal theory
Formally, a string is a finite, ordered sequence of characters such as letters, digits or spaces. The empty string is the special case where the sequence has length zero, so there are no symbols in the string.
There is only one empty string, because two strings are only different if they have different lengths or a different sequence of symbols.
In formal treatments,
The empty string should not be confused with the empty language
The empty string has several properties:
- |ε| = 0. Its string length is zero.
- ε ⋅ s = s ⋅ ε = s. The empty string is the identity element of the concatenation operation. The set of all strings forms a free monoid with respect to ⋅ and ε.
- εR = ε. Reversal of the empty string produces the empty string, so the empty string is a palindrome.
- . Statements that are about all characters in a string are vacuously true.
- The empty string precedes any other string under lexicographical order, because it is the shortest of all strings.[2]
In
Use in programming languages
In most programming languages, strings are a data type. Strings are typically stored at distinct memory addresses (locations). Thus, the same string (e.g., the empty string) may be stored in two or more places in memory.
In this way, there could be multiple empty strings in memory, in contrast with the formal theory definition, for which there is only one possible empty string. However, a string comparison function would indicate that all of these empty strings are equal to each other.
Even a string of length zero can require memory to store it, depending on the format being used. In most programming languages, the empty string is distinct from a
The empty string is usually represented similarly to other strings. In implementations with string terminating character (null-terminated strings or plain text lines), the empty string is indicated by the immediate use of this terminating character.
Different functions, methods, macros, or
λ representation | Programming languages |
---|---|
""
|
Visual Basic .NET
|
''
|
|
character(0)
|
R[3] |
{'\0'}
|
C, C++, Objective-C (as a C string) |
std::string()
|
C++ |
""s
|
C++ (since the 2014 standard) |
@""
|
Objective-C (as a constant NSString object)
|
[NSString string]
|
Objective-C (as a new NSString object)
|
q(), qq()
|
Perl |
str() [4]
|
Python |
%{} %()
|
Ruby |
String::new() [5]
|
Rust |
string.Empty
|
Visual Basic .NET
|
String.make 0 '-'
|
OCaml |
{}
|
Tcl |
[[]]
|
Lua
|
“”
|
PowerShell |
Representations of the empty string
![]() | This section needs expansion. You can help by adding to it. (March 2010) |
The empty string is a syntactically valid representation of
Zero-filled memory area, interpreted as a null-terminated string, is an empty string.
Empty lines of text show the empty string. This can occur from two consecutive
See also
References
- S2CID 2168826.
- ^ CSE1002 Lecture Notes – Lexicographic
- ^ There are two ways to create "empty strings" in R; the other is listed here as
""
.character(0)
creates empty character vectors, which will output 0 when counted. - ^ Another way to make an empty string is multiplying a string by 0 or a negative integer.
- ^ "String in std::string - Rust". doc.rust-lang.org. Retrieved 2022-11-30.