空文本框被认为是空字符串或null

时间:2022-11-13 19:50:08

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

所讨论的文本框包含在我代码中的if语句中,具有以下效果

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

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

我很好奇一个空的文本框会被认为是一个空字符串还是一个空语句。

6 个解决方案

#1


24  

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

尝试使用isnullorespace,这将确保也验证该空白,而不必对其进行修剪。

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

According to documentation string.IsNullOrWhiteSpace evaluates to :-

根据文档字符串。IsNullOrWhiteSpace评估:-

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

String.IsNullOrEmpty

String.IsNullOrEmpty

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

指示指定的字符串是否为空、空或仅由空白字符组成。

#2


4  

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

但是对于最佳实践,使用IsNullOrEmpty或IsNullOrWhiteSpace

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

}

Alternatively:

另外:

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

}    

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

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

#3


2  

It will be considered an empty string.

它将被认为是一个空字符串。

#4


2  

It will be an empty string but better to check with this IsNullOrEmpty or IsNullOrWhiteSpace

它将是一个空字符串,但最好使用这个IsNullOrEmpty或IsNullOrWhiteSpace进行检查

if (!string.IsNullOrEmpty(textbox.text))
{
  //do this
}

IsNullOrWhiteSpace is also take care of whitespace in input string. So if you don't want to execute the code for whitespace too then use second option.

IsNullOrWhiteSpace也会处理输入字符串中的空白。因此,如果您不想为空格执行代码,那么请使用第二个选项。

#5


-1  

string search = txtSearch.Text.Trim() != "" ? txtSearch.Text.Trim() : "0";

#6


-3  

if (textbox.text != "" || textbox.text != null)

如果(文本框。= " || textbox。文本! = null)

#1


24  

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

尝试使用isnullorespace,这将确保也验证该空白,而不必对其进行修剪。

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

According to documentation string.IsNullOrWhiteSpace evaluates to :-

根据文档字符串。IsNullOrWhiteSpace评估:-

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

String.IsNullOrEmpty

String.IsNullOrEmpty

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

指示指定的字符串是否为空、空或仅由空白字符组成。

#2


4  

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

但是对于最佳实践,使用IsNullOrEmpty或IsNullOrWhiteSpace

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

}

Alternatively:

另外:

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

}    

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

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

#3


2  

It will be considered an empty string.

它将被认为是一个空字符串。

#4


2  

It will be an empty string but better to check with this IsNullOrEmpty or IsNullOrWhiteSpace

它将是一个空字符串,但最好使用这个IsNullOrEmpty或IsNullOrWhiteSpace进行检查

if (!string.IsNullOrEmpty(textbox.text))
{
  //do this
}

IsNullOrWhiteSpace is also take care of whitespace in input string. So if you don't want to execute the code for whitespace too then use second option.

IsNullOrWhiteSpace也会处理输入字符串中的空白。因此,如果您不想为空格执行代码,那么请使用第二个选项。

#5


-1  

string search = txtSearch.Text.Trim() != "" ? txtSearch.Text.Trim() : "0";

#6


-3  

if (textbox.text != "" || textbox.text != null)

如果(文本框。= " || textbox。文本! = null)