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

Posted on

Question :

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 (“”)?


Answer :

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

string nome = "         ";
    //Sua função


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


Leave a Reply

Your email address will not be published. Required fields are marked *