How to calculate the length of the string using C#?

Use the String.Length property in C# to get the length of the string.


The property calculates the words in the string and displays the length of the specified string, for example, the string Amit has 4 characters −

string str = "Amit";


The following is the C# program to calculate the string length −

 Live Demo

using System;
using System.Collections;

namespace Demo {

   class Program {

      static void Main(string[] args) {

         string str = "Amit";

         Console.WriteLine("String: "+str);
         Console.WriteLine("String Length: "+str.Length);


String: Amit
String Length: 4