Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an empty textbox considered an empty string or null?

The text box in question is involved in an if statement within my code, something to the effect of

if (textbox.text != "") 
{
    do this
}

I am curious if an empty text box will be considered an empty string or a null statement.

like image 397
Rex_C Avatar asked May 17 '13 14:05

Rex_C


People also ask

IS NULL considered an empty string?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.

How do you check that TextBox is null or not?

IsNullOrEmpty() function has a boolean return type and returns true if the string is either null or empty and otherwise returns false . We can use the String. IsNullOrEmpty() function on the string inside the TextBox. Text property to check whether the text inside the text box is empty or not.

Is empty string same as null C#?

They are not the same thing and should be used in different ways. null should be used to indicate the absence of data, string. Empty (or "" ) to indicate the presence of data, in fact some empty text.

How check TextBox value is null in asp net?

You should use String. IsNullOrEmpty() to make sure it is neither empty nor null (somehow): if (String. IsNullOrEmpty(textBox1.


2 Answers

Try to use IsNullOrWhiteSpace, this will make sure of validating the whitespace too without having to trim it.

if (!string.IsNullOrWhiteSpace(textbox.Text))
{
    //code here
}

According to documentation string.IsNullOrWhiteSpace evaluates to:

return String.IsNullOrEmpty(value) || value.Trim().Length == 0;

String.IsNullOrWhiteSpace:

Indicates whether a specified string is null, empty, or consists only of white-space characters.

like image 155
PSL Avatar answered Oct 17 '22 06:10

PSL


In short it will be an empty string, but you could use the debugger and check that yourself.

However for best practice use IsNullOrEmpty or IsNullOrWhiteSpace

if (!string.IsNullOrEmpty(textbox.Text)) {

}

Alternatively:

if (!string.IsNullOrWhiteSpace(textbox.Text)) {

}    

http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx

like image 35
Darren Avatar answered Oct 17 '22 07:10

Darren