C# 如果 string 为 null 或为空,应该为 string.Split(";") 返回什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14815721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:05:46  来源:igfitidea点击:

what should be returned for string.Split(";") if string is null or empty

c#.net

提问by Developer

If string is empty or null,

如果字符串为空或为空,

Shouldn't string.split(";") should throw an error ?

string.split(";") 不应该抛出错误吗?

for me I am trying this code and goes through it without any error,

对我来说,我正在尝试此代码并通过它而没有任何错误,

string a = string.empty;

if (a.Split(';').Length - 1 < 1)

Can anyone tell me why it not throws an error and why if statement is true.

谁能告诉我为什么它不抛出错误以及为什么 if 语句为真。

采纳答案by SLaks

If the string is null, .Split()will (obviously) throw a NullReferenceException, like any other instance method.

如果字符串为空,.Split()则(显然)会NullReferenceException像任何其他实例方法一样抛出 a 。

If the string is empty, .Split()will return an array of a single empty string (unless you pass StringSplitOptions.RemoveEmptyEntries).
This is a corner case of its more general (and less unexpected) behavior; if the delimiter does not appear anywhere in the source string, it will return an array containing the entire source string.

如果字符串为空,.Split()将返回单个空字符串的数组(除非您通过StringSplitOptions.RemoveEmptyEntries)。
这是其更一般(且不太意外)行为的一个极端情况;如果分隔符没有出现在源字符串中的任何位置,它将返回一个包含整个源字符串的数组。

回答by Miles

From your code, a isn't null, it's String.Empty. So when you split an empty length string by a semicolon, there's 1 item. 1 - 1 is less than 1

从你的代码来看,a 不是空的,它是 String.Empty。所以当你用分号分割一个空长度的字符串时,有 1 个项目。1 - 1 小于 1

回答by David L

An empty string is NOT the same as a null string. Strings, being reference types will always contain "" if empty. Null is not at all the same thing, thus, if you have an empty string, it will have a length of 0 and your if statement will be valid.

空字符串与空字符串不同。字符串,作为引用类型,如果为空,将始终包含“”。Null 根本不是一回事,因此,如果您有一个空字符串,则它的长度为 0,并且您的 if 语句将有效。

回答by Jon Skeet

It should behave as documented:

它应该按照记录的方式运行:

If this instance does not contain any of the characters in separator, the returned array consists of a single element that contains this instance.

如果此实例不包含 中的任何字符separator,则返回的数组由包含此实例的单个元素组成。

An empty string clear does not contain any of the characters in separator, hence an array is returned consisting of a single element referring to an empty string.

空字符串 clear 不包含 中的任何字符separator,因此返回一个数组,其中包含引用空字符串的单个元素。

Of course, if you call Spliton a null reference, you'll get a NullReferenceException. It's important to differentiate between a reference to an empty string and a null reference.

当然,如果你调用Split一个空引用,你会得到一个NullReferenceException. 区分对空字符串的引用和空引用很重要。

If you want the method to return an emptyarray, use StringSplitOptions.RemoveEmptyEntries. If you want the result to be an error, you should check for this yourself and throw whatever exception you want.

如果您希望该方法返回一个数组,请使用StringSplitOptions.RemoveEmptyEntries. 如果您希望结果为错误,您应该自己检查并抛出您想要的任何异常。

It's important not to guess at behaviour when using an API though: if you're in any doubt at all, consult the documentation.

不过,在使用 API 时不要猜测行为很重要:如果您有任何疑问,请查阅文档。

回答by Sergey Brunov

The code splits the string into components separated with ';'- the result of this operation is the array of strings. If there are less than 2 componentsthe condition is true.

该代码将字符串拆分为分隔的组件';'- 此操作的结果是字符串数组。如果少于 2 个分量,则条件为真。