Pregunta

I want to ignore a line which is either empty , null or just have space or spaces (white spaces).the keyword here is multiple space. I have tried below codes without success

 if (!string.IsNullOrEmpty(line1))

or

if (line2 != "")

and I dont want to trim the file because I want to capture space space abc space space but not space space space space etc thanks

¿Fue útil?

Solución

.NET Framework 4:

string.IsNullOrWhiteSpace(str);

IsNullOrWhiteSpace is a convenience method that is similar to the following code, except that it offers superior performance:

return String.IsNullOrEmpty(value) || value.Trim().Length == 0;

IsNullOrWhiteSpace on MSDN

.NET Framework < 4:

you can use that line or:

if (value != null && value.Trim().Length > 0)
{...}    

Trim on MSDN

Otros consejos

String.IsNullOrWhiteSpace Method Indicates whether a specified string is null, empty, or consists only of white-space characters.
So it can detect if there are only spaces in the string.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top