简体   繁体   English

空字符串的目的是什么?

[英]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[] 对我来说,作为开发人员和用户,我发现空字符串(“”)无用且引起很多混乱,就像说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。

  • Null means that the value is undefined or unknown. Null表示该值未定义或未知。

  • 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. 你无法操纵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? 文本框的值应该为null还是空字符串? 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 "". 总是让文本框返回一个空字符串作为其值(当为空时)而不是尝试区分null和“”更容易。

Interestingly, some databases DO NOT differentiate between NULL and empty string - the best example of which is Oracle. 有趣的是,有些数据库不区分NULL和空字符串 - 最好的例子是Oracle。 In Oracle: 在Oracle中:

INSERT into MYTABLE(name) VALUES('')

actually inserts a NULL into the table. 实际上在表中插入一个NULL。 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 . 实际上,当编写从.NET中的数据库操作数据的代码时,这可能会产生令人困惑的情况 - 有时候你会得到DBNull.Value所在的String

Why do we need zero? 为什么我们需要零? It's really the same question. 这真的是同一个问题。 What is "pajama" without all the a's? 什么是“睡衣”没有所有的? "pjm". “PJM”。 What is "a" without all the a's? 什么是“a”没有所有的? "". “”。 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. 你可以将它连接到其他字符串,你可以计算其中的a,等等。

Sometimes you need a string value that isn't null if it isn't populated. 有时,如果未填充,则需要一个非null的字符串值。 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 "". 只是为了记录它,在C#中,鼓励在“”中指定string.Empty而不是硬编码。

Edit: And as LBushkin pointed out, there is a difference between have an empty value versus having an unknown value. 编辑:正如LBushkin指出的那样,有一个空值与一个未知值之间存在差异。

One could argue that the empty string is an instance of the Null Object Pattern . 有人可能会争辩说空字符串是Null对象模式的一个实例。 As Anthony pointed out, this is used to avoid special cases and lots of if s. 安东尼指出,这是为了避免特殊情况和大量的if秒。

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. 各种类似的代数定律适用:因为X + 0与X相同,Y cat“”与Y相同,等等。

Lets talk about substrings. 让我们谈谈子串。 If I have the string "abc", then 如果我有字符串“abc”,那么

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? 为什么我们需要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 如果我们不能表达任何东西的价值(这与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". 如果你说unknow plus(char)i,结果仍然是未知的,但是“”+“A”是“A”。 How big is 1 + unknown? 1 +未知有多大? when an addition can wrap around you can't even state that 1+A is larger than A (if A is unknown) 当一个加法可以缠绕你甚至不能说1 + A大于A(如果A未知)

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? 你为什么要在循环中的第一行抛出NullReferenceException而不是在没有更多字母的情况下结束循环?

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). 如果函数未能构造一个字符串,那么它可能返回一个null(取决于函数的契约 - 它可能抛出异常)。 In this case the null and the empty string have different meanings. 在这种情况下,null和空字符串具有不同的含义。

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). 另一方面是函数参数 - 如果字符串参数是可选的,那么你必须进行空检查(这是它通常与emptyString()检查捆绑在一起的地方)。

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 . null是特殊值,可以是任何对象的值,而空字符串是String Similar analogy can also be drawn between null and empty List or array. 类似的类比也可以在null和空List或数组之间绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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