有问题的文本框包含在我的代码中的 if 语句中,其效果为
if (textbox.text != "")
{
do this
}
我很好奇空文本框是否会被视为空字符串或空语句。
最佳答案
尝试使用 IsNullOrWhiteSpace
,这将确保验证空白,而无需修剪它。
if (!string.IsNullOrWhiteSpace(textbox.Text))
{
//code here
}
根据文档 string.IsNullOrWhiteSpace
计算为:
return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
Indicates whether a specified string is null, empty, or consists only of white-space characters.
关于c# - 空文本框是否被视为空字符串或 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16612057/