本文介紹了空文本框被視為空字符串還是 null?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
有問題的文本框包含在我的代碼中的 if 語句中,其效果是
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.
推薦答案
嘗試使用 IsNullOrWhiteSpace
,這將確保驗證空白也無需修剪.
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
}
根據文檔 string.IsNullOrWhiteSpace
計算結果為:
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.
這篇關于空文本框被視為空字符串還是 null?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!