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

CsharpProgrammingServer Side Programming

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
raja
Published on 07-Aug-2018 16:45:43
Advertisements