C# 文字列の文字数を表示する

  • 作成日 2021.01.04
  • 更新日 2022.03.04
  • C#
C# 文字列の文字数を表示する

C#で、Lengthを使用して、文字列の文字数を表示するサンプルコードを記述してます。

環境

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

Length使い方

Lengthを使用すると、文字列の文字数を取得することが可能です。

ここでは、アルファベットと平仮名と漢字で生成された文字列の文字数を取得してます。

using System;
using System.Collections.Generic;

namespace testapp
{
    class Program
    {
        static void Main(string[] args)
        {

            string str = "MEBEE";

            Console.WriteLine(str.Length);
            // 5

            str = "あいうえお";

            Console.WriteLine(str.Length);
            // 5

            str = "本日は晴天";

            Console.WriteLine(str.Length);
            // 5

            Console.ReadKey();
        }
    }
}