Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday 30 June 2015

STRINGS CHECK (EMPTYORNULL) IN C#


Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIV WIRE  +ASP.NET SOLUTIONS

Click imaginationhunt.blogspot to see latest Blogs
 
To read STRINGS (NULLOREMPTY) IN C# - Click here.


CHECK BLANK STRING

A blank string is a type of string which contains nothing. Nothing means not even a single character and whose length is zero.



Let's just understand this by an example.

Example- There is a simple user input program. In which the system ask user to input his name. And then it prints the name to the output screen.

Practical Implementation:

using System;

namespace AspnetSolutions
{
    class StringCheckNullEmpty
    {
        public static void Main()
        {

            Console.WriteLine("Enter You name");
            string name = Console.ReadLine();

            Console.WriteLine("Your name is = {0}.", name);

         }
    }
}


Output




Click imaginationhunt.blogspot to see latest Blogs
 
Hence, we get the output as expected.


But, suppose there is a user who didn't want to enter his name or leave it blank. And press Enter. Now, it's not correct to print the command like this- "Your name is = ."
So, to handle such situation, we check whether user has input some data string or not. And if not then we will alert it by saying "You have not entered your name". But the question arises how to do that?

4 Ways to Check String Null or Empty

Method1: Using "" (double quotes)

Blank string can be checked as str == "". "" (Double quotes) is the way to check blank string. Its return type is bool (i.e. true or false). Blank string only works when the length of string is zero.
Now, if user input an empty string, we would easily handle that by giving a message "You have not entered your name.".

Practical Implementation:

Click imaginationhunt.blogspot to see latest Blogs
 
using System;

namespace AspnetSolutions
{
    class StringCheckNullEmpty
    {
        public static void Main()
        {

            Console.WriteLine("Enter You name");
            string name = Console.ReadLine();

            if (name == "")
            {
                Console.WriteLine("Checking by blank string");
                Console.WriteLine("Length is = {0}",name.Length);
                Console.WriteLine("You have not entered your name.");
            }
            else
            {
                Console.WriteLine("Your name is = {0}.", name);
            }

         }
    }
}


Output


Here, we have placed an if-else block to check the condition. As the user left the name blank by pressing Enter. He automatically gets the alert "You have not entered your name." which we have placed in the overload of WriteLine() method.

Drawbacks:

1. This method will not work when string is null.
2. This method will not work for whitespaces. Implies if we give two spaces then this will not check empty string rather it may consider them as 2 characters. 


Click imaginationhunt.blogspot to see latest Blogs


Practical Implementation:

using System;

namespace AspnetSolutions
{
    class StringCheckNullEmpty
    {
        public static void Main()
        {

            // Checking when string is null
            string g = null;          
            Console.WriteLine(g == "");  // o/p is false


            // Checking when string has white spaces
            string h = "  ";
            Console.WriteLine(h == "");  // o/p is false

         }
    }
}


Output



As you can see the output for both null and white spaces string is False. Which means the double quotes check string does not work in these two cases.

To read STRINGS CHECK (EMPTYORNULL) IN C# part2 - Click here.

Related Questions:
Q-1 Which of the following option will be correct?
string email1="";
string email2=string.Empty;
string email3=null;

 

A) if (email1==email2)
    {
         Console.WriteLine("condition 1 is correct");
    }

B) if (email2 == email3)
    {
        Console.WriteLine("condition 2 is correct");
    }

C) if (email3 == email1)
    {
        Console.WriteLine("condition 3 is correct");
    }


Ans- Option(A).

Q-2 Which is the correct output for the piece of code?
string email2=string.Empty;
if (email2 != "")
{
    Console.WriteLine("condition successfully run");
}


Click imaginationhunt.blogspot to see latest Blogs

A) Code run. But print nothing.
B) condition successfully run
C) Compiler Error.
Ans- Option(A).

Keep learning and sharing...

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...