Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easier way of writing null or empty?

Tags:

I'm sure I've missed something here. With a certain project I need to check if a string is empty or null.

Is there an easier way of writing this?

if(myString == "" || myString == null) {    ... 
like image 906
Phil Avatar asked Oct 02 '11 12:10

Phil


People also ask

Is null or empty method?

C# | IsNullOrEmpty() Method In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.

What is the difference between null and empty?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.

Is string null or empty?

You can use the IsNullOrWhiteSpace method to test whether a string is null , its value is String. Empty, or it consists only of white-space characters.

Does isEmpty check for NULL?

isEmpty(<string>) Checks if the <string> value is an empty string containing no characters or whitespace. Returns true if the string is null or empty.


1 Answers

Yes, there's the String.IsNullOrEmpty helper method for exactly this already:

if (String.IsNullOrEmpty(myString)) {     ... } 
like image 65
Joey Avatar answered Sep 20 '22 02:09

Joey