How can I check whether a string variable is empty or null in C#?
Solution 1
if (string.IsNullOrEmpty(myString)) {
//
}
Solution 2
Since .NET 2.0 you can use:
// Indicates whether the specified string is null or an Empty string.
string.IsNullOrEmpty(string value);
Additionally, since .NET 4.0 there's a new method that goes a bit farther:
// Indicates whether a specified string is null, empty, or consists only of white-space characters.
string.IsNullOrWhiteSpace(string value);
Solution 3
if the variable is a string
bool result = string.IsNullOrEmpty(variableToTest);
if you only have an object which may or may not contain a string then
bool result = string.IsNullOrEmpty(variableToTest as string);
Solution 4
Cheap trick:
Convert.ToString((object)stringVar) == ""
This works because Convert.ToString(object)
returns an empty string if object
is null. Convert.ToString(string)
returns null if string
is null.
(Or, if you're using .NET 2.0 you could always using String.IsNullOrEmpty
.)
Solution 5
if (string.IsNullOrEmpty(myString))
{
. . .
. . .
}
Samantha J T Star
I'm in the Philippines so if you send some comment I may not be able to answer if I am sleeping :-) The good thing is I work all day until late each night.
Updated on November 24, 2021Comments
-
Samantha J T Star over 2 years
How can I check whether a C# variable is an empty string
""
or null?I am looking for the simplest way to do this check. I have a variable that can be equal to
""
or null. Is there a single function that can check if it's not""
or null? -
Adam Houldsworth over 12 yearsWhile technically correct I can categorically say I have never seen this method used.
-
molnarm about 12 yearsI had the same problem and the second one doesn't work correctly. Try this: object x=3; bool result = string.IsNullOrEmpty(x as string); 'x as string' will be null so the result is true despite x having a value other than null or an empty string. I didn't find a short solution, used a double check.
-
jk. about 12 years@MártonMolnár it would have to contain a string 3 is not a string so this is expected try using "3" instead
-
Shaiju T over 8 yearswhen i use
IsEmpty
it says:'string' does not contain a definition for IsEmpty
, can i useIsEmpty
in msdn or should i useIsNullOrEmpty
? -
Andrew Liu over 8 yearsVery simple and useful. I wish PHP could have something like this
-
Milan over 8 years@Lion Liu: Actually I think PHP has exactly as much to offer. See: php.net/manual/en/function.empty.php
-
Tony Trembath-Drake over 7 yearsI like IsNullOrWhiteSpace (null,empty, or whitespace...)
-
Stokely almost 7 yearsAre we to assume that this conversion of stringVar to a cast object returns empty string for both null and empty string assigned to the stringVar variable, but converting the same stringVar without the cast returns null and empty string instead? Im just trying to find out all the variations.....
-
chadpeppers over 6 years@AndrewLiu there is you simple do if (empty($my_var))
-
kayleeFrye_onDeck over 5 yearsJust to be super-clear, you have to use the class function for
string/String
, NOT trying to use the function via the object! Eg,string foo;
will not allow you to dofoo.IsNullOrEmpty();
; you need to use it likestring.IsNullOrEmpty(foo);
This is kind of annoying when coming from other languages that have built in null/0-length-checks for their string objects. -
Momoro over 4 years
stom
, your question was unclear. You just saidIsEmpty
, but that is not a validvariable
in the .NET C#