How to check an empty string in C#

Posted on 2012-08-29
Last Modified: 2012-08-30
Hi experts
I am trying to check for an empty string but I wonder why the following doesn't work.
  string s = "  "; // two empty spaces.
  bool flag = string.IsNullOrEmpty(s); // gives false.
Question by:dominicwong
    LVL 142

    Assisted Solution

    by:Guy Hengel [angelIII / a3]
    because "  " is not a empty string.

    you would need to check with s.Trim(), but then again, if s is null, that would raise a "objectnotset" exception.
    LVL 19

    Assisted Solution

    by:Rikin Shah
    LVL 43

    Assisted Solution

    If you require to check often that a string of blank spaces is 'empty' (as angelIII says it is not actually empty) then you might want to create your own string class based on the System.String or a function to do the job for you:

    bool IsNullOrEMPTY(String s)
      //test for null and return if null

      //trim blank spaces off the ends

      //test if empty and return
    LVL 12

    Accepted Solution


    Framework 4.0 has string method called IsNullOrWhiteSpace. It will return true if the specified string is null, empty, or consists only of white-space characters.
    string s = "  ";
    bool flag = string.IsNullOrWhiteSpace(s);

    Open in new window

    For older framework you should use
    string s = "  ";
    bool flag = String.IsNullOrEmpty(s) || s.Trim().Length == 0;

    Open in new window

    LVL 43

    Assisted Solution

    IsNullOrWhitespace - a blank is NOT the only whitespace character.
    LVL 7

    Assisted Solution


    Author Closing Comment

    I forgot to mention that mine is .NET 3.5
    Thanks to the prompts responses and help from everyone. I appreciate that.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video discusses moving either the default database or any database to a new volume.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now