gpt4 book ai didi

c# - 空文本框是否被视为空字符串或 null?

转载 作者:可可西里 更新时间:2023-11-01 07:51:32 24 4
gpt4 key购买 nike

有问题的文本框包含在我的代码中的 if 语句中,其效果为

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

我很好奇空文本框是否会被视为空字符串或空语句。

最佳答案

尝试使用 IsNullOrWhiteSpace,这将确保验证空白,而无需修剪它。

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

根据文档 string.IsNullOrWhiteSpace 计算为:

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.

关于c# - 空文本框是否被视为空字符串或 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16612057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com