CopyPastehas never been so tasty!

Check white spaces in string in C#

by anonymous

  • 0
  • 0
  • 0
79 views

Orignal source:http://codingvilla.com/check-white-spaces-string-csharp-single-article-615.aspx

 

White Spaces in String:

 

To check white spaces in string we use the method ‘IsNullOrWhiteSpace’and it is a static method. It checks the white spaces and new lines. It is a Boolean type method and takes a string as argument and if the string has white spaces it will return true otherwise it will return false.

 

To understand let us consider an example in which we have three strings, first one has no white but has an character and second has white spaces and third has \n which shows the new line, so if we check these strings by using ‘IsNullOrWhiteSpace’ method then the first string will return false and the other two strings will return true because these two strings have white spaces.

 

 

To demonstrate make a console application and write the following code.

 

 

Code:

 

 

classCheck_white_spaces_in_string

    {

staticvoid Main(string[] args)

        {

// Tests the string.IsNullOrWhiteSpace method three times.

string s = "a";

if (string.IsNullOrWhiteSpace(s))

            {

Console.WriteLine("No space");

            }

string s1 = "  ";

if (string.IsNullOrWhiteSpace(s1))

            {

Console.WriteLine("It has a space");

            }

string s2 = "\n";

if (string.IsNullOrWhiteSpace(s2))

            {

Console.WriteLine("It has a space");

 

            }

Console.ReadLine();

        }

}  

Add A Comment: