2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C# nullか空文字か空白か判定したい。

Posted at

【C# nullか空文字か空白か判定したい。】

C#で書かれた業務で使用するツールを改修しました。
その際に、null、空文字、空白を判定する必要がありました。
その判定に使用したメソッドを備忘録として残します。

IsNullOrWhiteSpaceメソッド

このメソッドを使用することで、文字列がnullか空文字か空白か判定可能。

下記が使用例です。

using System;

public class Example
{
  public static void Main()
  {  
    string example = "  ";  //空白  

    if (string.IsNullOrWhiteSpace(example))
    {
        Console.WriteLine("nullか空文字か空白である。");
    }
    else
    {
        Console.WriteLine("null、空文字、空白でない。");
    }
  }
}

似たようなメソッドでIsNullOrEmptyメソッドがある。

IsNullOrEmptyメソッドは、IsNullOrWhiteSpaceメソッドとほぼ同じ。
違う点としては、IsNullOrEmptyメソッドは空白を判定できない。
 → IsNullOrWhiteSpaceメソッドを使用すれば良いでしょう。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?