Erez
Erez

Reputation: 1953

The difference between null and empty string in .net

Trying to figure this out and couldn't find the answer anywhere. If i have a string and i split it in the spaces, like:

string[] arr_str = sentence.split(' ');

I will get an array with string. Now, if i enter double spaces in the sentence i will get arr_str cells with empty strings ("").

My question is why? why not with spaces???

When i do the check for the arr_str why do i need to check for empty string and not for null or spaces?

why only empty strings works?

Upvotes: 0

Views: 731

Answers (4)

Rosmarine Popcorn
Rosmarine Popcorn

Reputation: 10967

And empty string is a String with no Value ,and a null string is a String with no Reference(is not yet initiated). A null string does not take space into memory ,but an empty string does allocate space into memory.

Upvotes: 1

Tudor
Tudor

Reputation: 62439

If you enter double spaces, the split by space gets the string between two consecutive spaces which is an empty string. Like space space = space empty_string space.

Upvotes: 1

Niels
Niels

Reputation: 49919

You are splitting a string, which will results in strings.

if you split a double space, both spaces are removed but the emtpy string remains and that is your result.

Upvotes: 1

MK_Dev
MK_Dev

Reputation: 3333

It performs a split on one character, not including that character. As a result you're getting substrings that are between the split characters, in which case they're just empty strings.

You can, however, filter out empty strings if you want, by passing the appropriate enum the the method.

Upvotes: 1

Related Questions