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

Asked

Viewed 880 times

3

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

  • related: https://answall.com/questions/172694/qual-%C3%A9-a-difference%C3%A7a-entre-isnullorempty-e-isnullorpace whites

2 answers

7


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

string nome = "         ";
if(string.IsNullOrWhiteSpace(nome))
{
    //Sua função
}
  • only one comment: IsNullOrWhiteSpace is only available from . NET 3.5. But I believe it makes no difference =]

3

you can do so:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.