c-sharp
  1. c-sharp-program-to-find-the-length-of-a-string

Program to Find the Length of a String - (C# Basic Programs)

Finding the length of a string is a common task in programming. In this tutorial, we will discuss a program to find the length of a string using C# programming language.

Syntax

The syntax for finding the length of a string in C# is as follows:

string str = "example string";
int length = str.Length;

Example

using System;

namespace StringLength
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "example string";
            int length = str.Length;
            Console.WriteLine("The length of the string is: " + length);
        }
    }
}

Output:

The length of the string is: 14

Explanation

To find the length of a string in C#, we can use the .Length property. This property returns an int value that represents the number of characters in the string. In the example above, we define a string variable str with the value "example string". We then use the .Length property to find the length of the string and assign the result to an integer variable length. Finally, we use Console.WriteLine() to print the length of the string to the console.

Use

Finding the length of a string is a common task in programming, and it can be used in a variety of applications. For example, it can be used to ensure that user input data does not exceed a certain limit, or to manipulate strings in a specific way based on their length.

Summary

In this tutorial, we have discussed a program to find the length of a string using C# programming language. We have seen the syntax, example, explanation, use, and importance of finding the length of a string. This program is a basic example of string manipulation in C# and can be useful in a variety of applications.

Published on: