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


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

str.Length

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";

Example

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);
         Console.ReadKey();
      }
   }
}

Output

String: Amit
String Length: 4

Updated on: 20-Jun-2020

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements