C # - How to check if a string has only spaces?

3

The propgram reads all rows from a file, and I have to check that in this line that it is reading it has only spaces, because if it has only spaces in it, it will be considered invalid by the program. How do I check if it has only spaces ("")?

    
asked by anonymous 24.08.2017 / 21:41

2 answers

7

Simple, use: string.IsNullOrWhiteSpace(suaString) For example:

string nome = "         ";
if(string.IsNullOrWhiteSpace(nome))
{
    //Sua função
}
    
24.08.2017 / 21:44
3

You can do this:

string texto = "   ";
if (String.IsNullOrEmpty(texto) || texto.Trim().Length == 0)
{
   //a string só tem espaços ou não tem nada
}
    
24.08.2017 / 21:44