Home » C#.Net

String.IsNullOrEmpty() method with example in C#

C# String.IsNullOrEmpty() method: Here, we are going to learn about the IsNullOrEmpty() method of String class with example.
Submitted by IncludeHelp, on September 21, 2019

C# String.IsNullOrEmpty() Method

String.IsNullOrEmpty() method is a built-in method of String class and it is used to check whether a string is Null or Empty? If string object is not initialized with a correct value it will be considered as "null string", if string object is initialized but contains nothing i.e. it is assigned the value ("") it will be considered as "empty string".

Syntax:

    public static bool IsNullOrEmpty(String str);

The method is called with "string"/ "String". Here, "string" is an alias of "String" class.

Parameter(s):

  • str – represents a string value or string object to be checked.

Return value:

  • bool – it returns "True" if str is null or empty, otherwise it returns "False".

Example:

    Input:
    string str1 = "";
    string str2 = null;
    string str3 = "IncludeHelp";
    
    Function call
    Console.WriteLine(string.IsNullOrEmpty(str1));
    Console.WriteLine(string.IsNullOrEmpty(str2));
    Console.WriteLine(string.IsNullOrEmpty(str3));

    Output:
    True
    True
    False

C# Example to convert string to characters array using String.IsNullOrEmpty() method

Example 1:

using System;
class IncludeHelp
{
    static void Main()
    {
        // declaring string variables
        string str1 = "";
        string str2 = null;
        string str3 = "IncludeHelp";

        // check whether string is empty/null or not
        Console.WriteLine(string.IsNullOrEmpty(str1));
        Console.WriteLine(string.IsNullOrEmpty(str2));
        Console.WriteLine(string.IsNullOrEmpty(str3));
    }
}

Output

True
True
False

Example 2:

using System;

class IncludeHelp
{
    static void Main()
    {
        // declaring string variable
        string str = "IncludeHelp";

        // checking whether string is null/empty or not
        if(string.IsNullOrEmpty(str))
            Console.WriteLine("str is empty or null");
        else
            Console.WriteLine("str is not empty or null");

        //now assigning null to the string
        str = null;

        // checking whether string is null/empty or not
        if(string.IsNullOrEmpty(str))
            Console.WriteLine("str is empty or null");
        else
            Console.WriteLine("str is not empty or null");

    }
}

Output

str is not empty or null
str is empty or null
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.