C# 文字列の文字数がゼロであることを判定する

  • 作成日 2021.01.07
  • 更新日 2022.03.04
  • C#
C# 文字列の文字数がゼロであることを判定する

C#で、IsNullOrEmptyを使用して、文字列の文字数がゼロであることを判定するサンプルコードを記述してます。

環境

  • OS windows10 pro 64bit
  • Microsoft Visual Studio Community 2019 Version 16.7.1

IsNullOrEmpty使い方

IsNullOrEmptyを使用すると、文字列の文字数がゼロであることを判定することが可能です。

using System;
using System.Collections.Generic;

namespace testapp
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "mebee";
            string str2 = "";

            Console.WriteLine(String.IsNullOrEmpty(str1));
            // False
            Console.WriteLine(String.IsNullOrEmpty(str2));
            // True
        }
    }
}