检查文本框是否为空 [英] Check Whether a TextBox is empty or not
本文介绍了检查文本框是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个TextBox.我想检查一下是否为空.
I have a TextBox. And I want to check if it's empty.
哪种方法更好
if(TextBox.Text.Length == 0)
或
if(TextBox.Text == '')
?
推荐答案
You should use String.IsNullOrEmpty()
to make sure it is neither empty nor null (somehow):
if (String.IsNullOrEmpty(textBox1.Text))
{
// Do something...
}
更多示例此处.
出于实际目的,您可能还考虑使用 String.IsNullOrWhitespace()
,因为期望将空格作为输入的TextBox可能会抵消任何目的,除非例如让用户选择自定义分隔符东西.
For practical purposes you might also consider using String.IsNullOrWhitespace()
since a TextBox expecting whitespace as input probably negates any purpose, except in case of, say, letting the user pick a custom separator for stuff.
这篇关于检查文本框是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文