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
To read STRINGS CHECK (EMPTYORNULL) IN C# Part1 - Click here.
CHECK EMPTY STRING (Cont'd)
We'll follow the same example to understand the remaining 3 Methods.
Example- There is a simple user input program. In which the system asks the user to input his name. And then it prints the name to the output screen.
Method2: Check Using String.Empty
Empty is an object of class String. This object is used to read the string in case when it is empty. It returns its object in an empty string format which is then compared with the specified string. If the condition returns 'True' means string is empty but if it returns 'False' means the string contains some characters.
Click imaginationhunt.blogspot to see latest Blogs
CHECK EMPTY STRING (Cont'd)
We'll follow the same example to understand the remaining 3 Methods.
Example- There is a simple user input program. In which the system asks the user to input his name. And then it prints the name to the output screen.
Method2: Check Using String.Empty
Empty is an object of class String. This object is used to read the string in case when it is empty. It returns its object in an empty string format which is then compared with the specified string. If the condition returns 'True' means string is empty but if it returns 'False' means the string contains some characters.
Practical Implementation:
using System;
namespace AspnetSolutions
{
class StringCheckNullEmpty
{
public static void Main()
{
Console.WriteLine("Enter You name");
string name = Console.ReadLine();
if (name == String.Empty)
{
Console.WriteLine("Checking by string.Empty");
Console.WriteLine("Length is = {0}",name.Length);
Console.WriteLine("You have not entered your name.");
}
else
{
Console.WriteLine("Your name is = {0}.", name);
}
}
}
}
Output
Drawbacks:
1. This method will not work when string is null.
2. This method will not work for white spaces. 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
Method3: Check Using IsNullOrEmpty()
IsNullOrEmpty() method is an object of String class. This method is used to tell whether the string is null or String.Empty string. This method accepts a string which has to pass as parameter. Its return type is bool means it returns 'True' when the string is empty or null and return 'False' when the string is neither null nor empty.
Practical Implementation:
using System;
namespace AspnetSolutions
{
class StringCheckNullEmpty
{
public static void Main()
{
Console.WriteLine("Enter You name");
string name = Console.ReadLine();
if (String.IsNullOrEmpty(name))
{
Console.WriteLine("Checking by string.IsNullOrEmpty()");
Console.WriteLine("Length is = {0}",name.Length);
Console.WriteLine("You have not entered your name.");
}
else
{
Console.WriteLine("Your name is = {0}.", name);
}
}
}
}
Output
Click imaginationhunt.blogspot to see latest Blogs
Drawbacks:
1. This method will not work for white spaces. Implies if we give two spaces, then this will not check empty string rather it may consider them as 2 characters.
Method4: Check Using IsNullOrWhiteSpace()
IsNullOrWhiteSpace() method is an object of String class. This method is used to tell whether the string is null or String.Empty string or having white space characters. This method accepts a string which has to pass as parameter. Its return type is bool means it returns 'True' when the string is empty or null or having white spaces and return 'False' when the string is neither null nor empty nor having white space characters.
Practical Implementation:
string name = Console.ReadLine();
if (name == String.Empty)
{
Console.WriteLine("Checking by string.Empty");
Console.WriteLine("Length is = {0}",name.Length);
Console.WriteLine("You have not entered your name.");
}
else
{
Console.WriteLine("Your name is = {0}.", name);
}
}
}
}
Output
Drawbacks:
1. This method will not work when string is null.
2. This method will not work for white spaces. 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
Method3: Check Using IsNullOrEmpty()
IsNullOrEmpty() method is an object of String class. This method is used to tell whether the string is null or String.Empty string. This method accepts a string which has to pass as parameter. Its return type is bool means it returns 'True' when the string is empty or null and return 'False' when the string is neither null nor empty.
Practical Implementation:
using System;
namespace AspnetSolutions
{
class StringCheckNullEmpty
{
public static void Main()
{
Console.WriteLine("Enter You name");
string name = Console.ReadLine();
if (String.IsNullOrEmpty(name))
{
Console.WriteLine("Checking by string.IsNullOrEmpty()");
Console.WriteLine("Length is = {0}",name.Length);
Console.WriteLine("You have not entered your name.");
}
else
{
Console.WriteLine("Your name is = {0}.", name);
}
}
}
}
Output
Click imaginationhunt.blogspot to see latest Blogs
Drawbacks:
1. This method will not work for white spaces. Implies if we give two spaces, then this will not check empty string rather it may consider them as 2 characters.
Method4: Check Using IsNullOrWhiteSpace()
IsNullOrWhiteSpace() method is an object of String class. This method is used to tell whether the string is null or String.Empty string or having white space characters. This method accepts a string which has to pass as parameter. Its return type is bool means it returns 'True' when the string is empty or null or having white spaces and return 'False' when the string is neither null nor empty nor having white space characters.
Practical Implementation:
using System;
namespace AspnetSolutions
{
class StringCheckNullEmpty
{
public static void Main()
{
Console.WriteLine("Enter You name");
string name = Console.ReadLine();
if (String.IsNullOrWhiteSpace(name))
{
Console.WriteLine("Checking by string.IsNullOrWhiteSpace()");
Console.WriteLine("Length is = {0}",name.Length);
Console.WriteLine("You have not entered your name.");
}
else
{
Console.WriteLine("Your name is = {0}.", name);
}
}
}
}
Click imaginationhunt.blogspot to see latest Blogs
Output
This will work for all the cases. And hence best to use this method.
To read MODIFY STRINGS C# - Click here.
Related Questions:
Q-1 Which of the following options are correct?
using System;
namespace AspnetSolutions
{
class StringCheckNullEmpty
{
public static void Main()
{
string salary = string.Empty;
Console.WriteLine(salary == "");
Console.WriteLine(salary == string.Empty);
Console.WriteLine(string.IsNullOrEmpty(salary));
Console.WriteLine(string.IsNullOrWhiteSpace(salary));
using System;
namespace AspnetSolutions
{
class StringCheckNullEmpty
{
public static void Main()
{
string salary = string.Empty;
Console.WriteLine(salary == "");
Console.WriteLine(salary == string.Empty);
Console.WriteLine(string.IsNullOrEmpty(salary));
Console.WriteLine(string.IsNullOrWhiteSpace(salary));
}
}
}
A) True, True, True, True
B) True, False, True, False
C) True, True, True, False
D) True, False, False, True
Ans- Option(A).
Click imaginationhunt.blogspot to see latest Blogs
Q-2 We have passed null escape sequence('\0') as string. Find out which of the conditions will return 'True'.
using System;
namespace AspnetSolutions
{
class StringCheckNullEmpty
{
public static void Main()
{
string salary = "\0";
Console.WriteLine(salary); // o/p is
Console.WriteLine(salary == string.Empty); //Condition 1
Console.WriteLine(string.IsNullOrEmpty(salary)); //Condition 2
Console.WriteLine(string.IsNullOrWhiteSpace(salary)); //Condition 3
}
}
}
A) Condition 3 only
B) Condition 1 and 2
C) Condition 1 and 3
D) None of the Above
Ans- Option(D).
Click imaginationhunt.blogspot to see latest Blogs
}
}
A) True, True, True, True
B) True, False, True, False
C) True, True, True, False
D) True, False, False, True
Ans- Option(A).
Click imaginationhunt.blogspot to see latest Blogs
Q-2 We have passed null escape sequence('\0') as string. Find out which of the conditions will return 'True'.
using System;
namespace AspnetSolutions
{
class StringCheckNullEmpty
{
public static void Main()
{
string salary = "\0";
Console.WriteLine(salary); // o/p is
Console.WriteLine(salary == string.Empty); //Condition 1
Console.WriteLine(string.IsNullOrEmpty(salary)); //Condition 2
Console.WriteLine(string.IsNullOrWhiteSpace(salary)); //Condition 3
}
}
}
A) Condition 3 only
B) Condition 1 and 2
C) Condition 1 and 3
D) None of the Above
Ans- Option(D).
Click imaginationhunt.blogspot to see latest Blogs
Keep learning and sharing...
No comments:
Post a Comment