C# checking white space in a textbox

后端 未结 9 857
孤独总比滥情好
孤独总比滥情好 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:20
    if (String.IsNullOrWhiteSpace(txtBox.Text))
    {
        // so stuff
    }
    
    0 讨论(0)
  • 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 == " ";
    
    0 讨论(0)
  • 2021-01-03 07:27
            var Rxwhitesp = new Regex(@"\s");
    
            string textboxstring = textbox.Text;
            string textboxfirststring = textbox.Text.First().ToString();
            if (Rxwhitesp.IsMatch(textboxfirststring) && (textboxstring.Length == 1))
            {
                // write code for true condition
            }
            else
            {
                // write code for false condition
            }
    
    0 讨论(0)
提交回复
热议问题