Click here to Skip to main content
13,254,187 members (60,556 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 17 May 2010

.NET 4: string.IsNullOrWhitespace()

, 17 May 2010
Rate this:
Please Sign up or sign in to vote.
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
bool string.IsNullOrEmpty(<string_val>)

which handled first two conditions for us.

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

Basically it is equivalent to the following code,
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 !


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


About the Author

Sarang Date
Technical Lead Microsoft India R&D Pvt. Ltd. Hyderabad
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
soulprovidergr17-Sep-12 0:15
membersoulprovidergr17-Sep-12 0:15 
GeneralAwesome Pin
Manuel_Perez_II6-Aug-10 10:20
memberManuel_Perez_II6-Aug-10 10:20 
GeneralComparison vs input.trim Pin
supercat910-Jun-10 6:50
membersupercat910-Jun-10 6:50 
I would expect that String.IsNullOrWhitespace would outperform String.Trim in the case where a string isn't all whitespace, since in that case the latter has to do some work building a new string. I'm curious, though, how its performance compares in the case where a string consists entirely of various amounts of whitespace. Since the circumstances where one would be most interested in whether a string has any non-whitespace characters would be those in which one planned to strip such characters from a string, I would think the most common useful pattern would be to set a string equal to whateverString.trim (after checking for null). If that string is empty, skip it; otherwise, process it.
GeneralCool Pin
Evgeniy Stepanow20-May-10 12:32
memberEvgeniy Stepanow20-May-10 12:32 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 18 May 2010
Article Copyright 2010 by Sarang Date
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid