Click here to Skip to main content
15,878,871 members
Articles / General Programming / String
Tip/Trick

.NET 4: string.IsNullOrWhitespace()

Rate me:
Please Sign up or sign in to vote.
4.68/5 (17 votes)
17 May 2010CPOL 29.5K   7   4
Any string input can have these many possible states1) String is null2) String is empty3) String contains nothing but white space4) String has some contentTill now, .NET had static method for stringbool string.IsNullOrEmpty()which handled first two conditions for...
Any string input can have these many possible states
1) String is null
2) String is empty
3) String contains nothing but white space
4) String has some content

Till now, .NET had static method for string
C#
bool string.IsNullOrEmpty(<string_val>)

which handled first two conditions for us.

Now with .NET 4 we have another static method
C#
bool string.IsNullOrWhitespace(<string_val>)


Basically it is equivalent to the following code,
C#
return String.IsNullOrEmpty(input) || input.Trim().Length ==0 ;


but has performance improvement because white-space characters are defined by the Unicode standard. The IsNullOrWhiteSpace method interprets any character that returns a value of true when it is passed to the Char.IsWhiteSpace method as a white-space character.

We must have implemented logic for this condition everywhere in our applications but addition of this useful inbuilt function is nice to have with some performance improvement !

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Microsoft India R&D Pvt. Ltd. Hyderabad
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
soulprovidergr16-Sep-12 23:15
soulprovidergr16-Sep-12 23:15 
GeneralAwesome Pin
Mico Perez6-Aug-10 9:20
Mico Perez6-Aug-10 9:20 
GeneralComparison vs input.trim Pin
supercat910-Jun-10 5:50
supercat910-Jun-10 5:50 
GeneralCool Pin
Evgeniy Stepanow20-May-10 11:32
Evgeniy Stepanow20-May-10 11:32 
It has appeared useful to me. Thanks!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.