C# checking white space in a textbox

后端 未结 9 880
孤独总比滥情好
孤独总比滥情好 2021-01-03 06:45

How can I check in C# that is there a white space only in a textbox and perform some operation after that?

9条回答
  •  悲哀的现实
    2021-01-03 07:25

    This ensures multiple spaces are caught in your check.

     bool hasAllWhitespace = txtBox1.Text.Length>0 &&
                             txtBox1.Text.Trim().Length==0;
    

    To check for a single space only:

     bool hasSingleWhitespace = txtBox1.Text == " ";
    

提交回复
热议问题