Recipe2.24.Testing a String for Null or Empty


Recipe 2.24. Testing a String for Null or Empty

Problem

You need a quick and easy way to check if a string is either null or of zero length.

Solution

Use the static IsNullOrEmpty method of the String class:

 bool stringTestResult = String.IsNullOrEmpty(testString); 

Discussion

The IsNullOrEmpty method is a very convenient method in that it allows you to test a string for null or zero length with a single method call. This method returns true if the string passed in to it is equal to one of the following:

  • Null

  • String.Empty

Otherwise, this method returns false.

See Also

See the "String.IsNullOrEmpty Method" topic in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net