C# - Get the Length of a String

Given a string, we have to find it length using C# program.
Submitted by Nidhi, on October 10, 2020 [Last updated : March 21, 2023]

Here, we read a string from the keyboard and then get the length of the string using the Length property.

C# program to get the length of a string

The source code to get the length of the string in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# - Get the Length of a String.

using System;

class Demo
{
    static void Main()
    {
        string str = "";

        Console.Write("Enter the string: ");
        str = Console.ReadLine();

        Console.WriteLine("Length of string: " + str.Length);
    }
}

Output

Enter the string: www.includehelp.com
Length of string: 19
Press any key to continue . . .

Explanation

Here, we created a Demo class that contains the Main() method. The Main() method is the entry point of the program. Here we read a string, and then find the length of the string using the Length property of String class that will be printed on the console screen.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.