C# checking white space in a textbox

后端 未结 9 856
孤独总比滥情好
孤独总比滥情好 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:03
    if (txtBox.Text.equals(" ")))
    {
     // your code goes here
    }
    
    0 讨论(0)
  • 2021-01-03 07:05
    //SIMPLE WAY TO VALIDATE EMPTY SPACES
    if (txtusername.Text.Contains(" "))
    {
        MessageBox.Show("Invalid Username");
        txtusername.Clear();
        txtusername.Focus();
    }
    
    0 讨论(0)
  • 2021-01-03 07:07
    txtBox.Text.Length == 1 && char.IsWhiteSpace( txtBox.Text.First() );
    
    0 讨论(0)
  • 2021-01-03 07:08

    Check the Text property of the textbox using string.IsNullOrWhiteSpace.

    if (string.IsNullOrWhiteSpace(myTextBox.Text) && myTextBox.Text.Length > 0)
    {
      // do stuff
    }
    

    Since IsNullOrWiteSpace will return true if the textbox is empty (or the property is null), adding the Length check ensures that there is something within the text box. The combination of the tests ensures a true if there is only whitespace in the textbox.

    0 讨论(0)
  • 2021-01-03 07:16

    Some LINQ fun:

    bool isWhitespace = txtBox.Text.All(char.IsWhiteSpace);
    
    0 讨论(0)
  • 2021-01-03 07:19
    if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, @"\s",)) {
        // do your code
    }
    
    0 讨论(0)
提交回复
热议问题