Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can String.Split() ever return null? (.net)

Tags:

string

c#

.net

Does System.String.Split() ever return null? (.NET)

I know I've been coding in the belief that it does not, however, upon reading the docs I do not see such a statement. Since there is no such a statement in the docs, so I want to ask in the experience of the community has anyone actually encountered the case that string.Split returns null?

like image 535
Tim Lovell-Smith Avatar asked Jun 13 '12 19:06

Tim Lovell-Smith


People also ask

What does split method return?

The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Does split return a string?

The split() method does not change the value of the original string. If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.

What does split () return if the string has no match C#?

split (separator, limit) , if the separator is not in the string, it returns a one-element array with the original string in it.

What does string split do in C#?

In C#, Split() is a string class method. The Split() method returns an array of strings generated by splitting of original string separated by the delimiters passed as a parameter in Split() method. The delimiters can be a character or an array of characters or an array of strings.


2 Answers

No, it cannot return null. If you look at the source of it, it even guarantees it with code contracts:

public String[] Split(params char[] separator) {     Contract.Ensures(Contract.Result<String[]>() != null); 

All public overloads also make the same guarantee.

like image 187
vcsjones Avatar answered Sep 28 '22 23:09

vcsjones


No, it doesn't return null. If the separator is not present, it returns the whole string

From MSDN

If this instance does not contain any of the strings in separator, the returned array consists of a single element that contains this instance. If the separator parameter is null or contains no characters, white-space characters are assumed to be the delimiters

like image 29
Claudio Redi Avatar answered Sep 28 '22 22:09

Claudio Redi