Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | IsNullOrWhiteSpace() Method
Next article icon

C# | IsNullOrWhiteSpace() Method

Last Updated : 31 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
In C#, IsNullOrWhiteSpace() is a string method. It is used to check whether the specified string is null or contains only white-space characters. A string will be null if it has not been assigned a value or has explicitly been assigned a value of null. Syntax:
  public static bool IsNullOrWhiteSpace(String str)    
Explanation: This method will take a parameter which is of type System.String and this method will return a boolean value. If the method's parameter list is null or String.Empty, or only contains white-space characters then return True otherwise return False. Example:
  Input : str  = null         // initialize by null value          String.IsNullOrWhiteSpace(str)  Output: True    Input : str  = " "  // initialize by whitespace          String.IsNullOrWhiteSpace(str)  Output: True  
Program: To demonstrate the working of the IsNullOrWhiteSpace() Method : csharp
// C# program to illustrate  // IsNullOrWhiteSpace() Method using System; class Geeks {        // Main Method     public static void Main(string[] args)     {         string s1 = null;          // for null value always return true         bool b1 = String.IsNullOrWhiteSpace(s1);         Console.WriteLine(b1);          string s2 = " ";          // for whitespace value always return true         bool b2 = String.IsNullOrWhiteSpace(s2);         Console.WriteLine(b2);          string s4 = " \n ";          // for new line value return true         bool b4 = String.IsNullOrWhiteSpace(s4);         Console.WriteLine(b4);          string s5 = "\t";          // for tab value return true         bool b5 = String.IsNullOrWhiteSpace(s5);         Console.WriteLine(b5);          string s6 = "\r";          // for carriage Return value return true         bool b6 = String.IsNullOrWhiteSpace(s6);         Console.WriteLine(b6);          string s7 = "GFG";          // for s7 it return False         bool b7 = String.IsNullOrWhiteSpace(s7);         Console.WriteLine(b7);     } } 
Output:
  True  True  True  True  True  False  
Note: There is a alternative code of IsNullOrWhiteSpace() method as follows:
  return String.IsNullOrEmpty(str) || str.Trim().Length == 0;  
Program: To demonstrate IsNullOrEmpty() method’s alternative CSharp
// C# program to illustrate the  // similar code for IsNullOrWhiteSpace() using System; class Geeks {      // similar code to      // IsNullOrWhiteSpace()     public static bool check(string str)     {         return(String.IsNullOrEmpty(str) ||                str.Trim().Length == 0) ? true : false;     }      // Main Method     public static void Main(string[] args)     {         string s1 = "GeeksforGeeks";         string s2 = " ";          string s3 = null;         string s4 = " \n ";          bool b1 = check(s1);         bool b2 = check(s2);         bool b3 = check(s3);         bool b4 = check(s4);          // To display result         Console.WriteLine(b1);         Console.WriteLine(b2);         Console.WriteLine(b3);         Console.WriteLine(b4);     } } 
Output:
  False  True  True  True  
Reference: https://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace

Next Article
C# | IsNullOrWhiteSpace() Method

M

Mithun Kumar
Improve
Article Tags :
  • Misc
  • C#
  • CSharp-method
  • CSharp-string
Practice Tags :
  • Misc

Similar Reads

    C# | Char.IsWhiteSpace() Method
    In C#, Char.IsWhiteSpace() is a System.Char struct method which is used to check whether a Unicode character is a whitespace or not. Whitespace characters include Unicode characters of category SpaceSeparator, LineSeparator, ParagraphSeparator etc. This method can be overloaded by passing different
    3 min read
    C# String 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.Empty (A constant for empty strings).Example 1: Using IsNullOrEmpty
    2 min read
    C# | Char.IsLower() Method
    In C#, Char.IsLower() is a System.Char struct method which is used to check whether a Unicode character can be categorized as a lowercase letter or not. Valid lowercase letters will be the members of UnicodeCategory: LowercaseLetter. This method can be overloaded by passing different type and number
    3 min read
    C# | Char.IsLetter() Method
    In C#, Char.IsLetter() is a System.Char struct method which is used to check whether a Unicode character can be categorized as a Unicode letter or not. Unicode letters consist of the Uppercase letters, Lowercase letters, Title case letters, Modifiers letters and Other letters. This method can be ove
    3 min read
    C# | Char.IsNumber() Method
    In C#, Char.IsNumber() is a System.Char struct method which is used to check whether a Unicode character can be categorized as a number or not. Valid numbers will be the members of the UnicodeCategory.DecimalDigitNumber, UnicodeCategory.LetterNumber, or UnicodeCategory.OtherNumber category. This met
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences