简体   繁体   中英

What's the purpose of the empty string?

To me, as a developer and as a user, I find the empty string ("") useless and the cause of much confusion, it's like saying string == char[]

Maybe computers need the empty string, so I'd like to learn why.

See also:

Empty string are quite common in real world situations - and they are useful to differentiate from null.

  • Null means that the value is undefined or unknown.

  • An empty string means that it is known to be nothing (empty).

It might seem like splitting hairs, but there is a difference. Empty strings are instances of an object - nulls are not. You can manipulate an empty string using any of the valid methods that can be called on a string object. You cannot manipulate a null. Empty strings are often the result of certain manipulations on other strings, for example:

var source = "Hello";
var result = source.Remove("Hello");  // result is now: ""

Here's a simple example where the above matters: an application with a form that collects a persons's middle name via a textbox. Should the value of the text box be null or empty string? It's much easier to always have the textbox return an empty string as its value (when empty) rather than trying to differentiate between null and "".

Interestingly, some databases DO NOT differentiate between NULL and empty string - the best example of which is Oracle. In Oracle:

INSERT into MYTABLE(name) VALUES('')

actually inserts a NULL into the table. This can in fact create confusing situations when writing code that manipulates data from a database in .NET - you sometimes get DBNull.Value where you expected a String .

Why do we need zero? It's really the same question. What is "pajama" without all the a's? "pjm". What is "a" without all the a's? "". Simple as that. The product of some string operations is a string without any characters in it, and that's the empty string. It behaves just like any other string - you can easily determine its length; you can concatenate it onto other strings, you can count the a's in it, etc.

Sometimes you need a string value that isn't null if it isn't populated. Otherwise, for any given string-related task you need to perform, you have to include a null check which merely serves to bloat your code.

And just to have it on record, in C#, it is encouraged to specify string.Empty instead of hardcoding in "".

Edit: And as LBushkin pointed out, there is a difference between have an empty value versus having an unknown value.

One could argue that the empty string is an instance of the Null Object Pattern . As Anthony pointed out, this is used to avoid special cases and lots of if s.

It serves the same purpose for strings as the number "zero" serves for integers. If you didn't have zero, you couldn't write down the number of sheep you have when you don't have any sheep. Likewise, if you don't have the empty string, you can't write down the seqeunce of characters you have, when you don't have any characters.

All sorts of analoguous algebraic laws apply: as X+0 is the same as X, Y cat "" is the same as Y, etc.

Lets talk about substrings. If I have the string "abc", then

substring("abc",1,3)  is  "abc"
substring("abc",1,2)  is  "ab"
substring("abc",1,1)  is  "a"
substring("abc",1,0)  is ...?

Another interesting case:

substring("abc",1,3) is "abc"
substring("abc",2,2) is "bc"
substring("abc",3,1) is "c"
substring("abc",4,0) is ...?

A lot of programming languages get this last case wrong, because an implementer didn't think the empty string was interesting.

why do we need xero? or an empty array, an empty set (also called a null set). Why do we care at all about things when they are not there?

Somethings are simply more interesseting when they are not present

Somethings cannot be expressed if we cannot express the value of nothing (which is very differrent from the "unknown value" of null

eg

string allUpperCaseLetters = "";
for(byte i = 'A';i<'Z';i++)
{
  allUpperCaseLetters += (char)i;
}

if you say unknow plus (char)i the result would still be unknown however "" + 'A' is "A". How big is 1 + unknown? when an addition can wrap around you can't even state that 1+A is larger than A (if A is unknown)

some operations would be very weird if we didn't have "" how about

string letters = allUpperCaseLetters;
for(;i>0;)
{
  int i = letters.Length
  Console.WriteLine(letters[0]);
  letters = letters.Substring(1);
}

why would you want the first line inside the loop to throw a NullReferenceException rather than the loop just ending when there's no more letters?

If an empty string is returned from a function, it means the function carried out it's purpose and the result was an empty string. If the function failed to construct a string, then it could return a null (depending on the function's contract - it may throw an exception). In this case the null and the empty string have different meanings.

The flipside is function parameters - if a string param is optional, then you have to do the null check (this is where it's usually bundled in with the emptyString() check).

I have'nt heard complaining about this one before. While null is special value which can be a value of any object while empty string is a String . Similar analogy can also be drawn between null and empty List or array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM