C# String Length

To get the length of a String, use Length property on string.

string.Length

string.Length returns an integer that represents the number of characters in the string.

Example – Get String Length in C#

In the following example, we will read a string from console and find its length.

Program.cs

using System;

namespace CSharpExamples {

    class Program {
        static void Main(string[] args) {
            Console.Write("Enter a string : ");
            string str = Console.ReadLine();
            int len = str.Length;
            Console.WriteLine("Length of the string is : "+len);
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
Enter a string : TutorialKart
Length of the string is : 12
PS D:\workspace\csharp\HelloWorld> dotnet run
Enter a string : Welcome to C# Tutorial.
Length of the string is : 23

Conclusion

In this C# Tutorial, we learned about C# String.Length property with the help of example programs.