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.
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.
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.
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.
You should use String. IsNullOrEmpty() to make sure it is neither empty nor null (somehow): if (String. IsNullOrEmpty(textBox1.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With