C# String Length – Get the Length of a String

How to get the length of a string

This tutorial explains how to get the string length in C#.

Once you know the length of a string, you can use it in string methods such as Remove(), Replace(), and Substring().

C# String Contains Substring

String length syntax

To get the length of a string, use the following syntax

string.Length

For example:
string name = “James”;
int length = name.Length;

C# String Length Examples

Example 1:

The following sample program returns the length of a string named "textString":

  1. using System;
  2. namespace CSharpExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string textString = "C# is one of the most popuplar programming language. "
  9. + "With C#, you can develop mobile apps, desktop applicaitons, machine learning tools, "
  10. + "and web applicaitons";
  11. //Get the length of the string.
  12. int length = textString.Length;
  13. //Write to the console.
  14. Console.WriteLine("The length of the string: {0}", length);
  15. }
  16. }
  17. }

Output:

The length of the string: 157

Example 2:

The following sample program is an example of using string.Length and the string.Remove() method.

  1. using System;
  2. namespace CSharpExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string textString = "C# is one of the most popuplar programming language. "
  9. + "With C#, you can develop mobile apps, desktop applicaitons, machine learning tools, "
  10. + "and web applicaitons";
  11. int length = textString.Length;
  12. //Remove the last 10 characters
  13. string newString = textString.Remove(length - 10);
  14. Console.WriteLine("The new string: {0}", newString);
  15. }
  16. }
  17. }

Output:

The new string: C# is one of the most popuplar programming language. With C#, you can develop mobile apps, desktop applicaitons, machine learning tools, and web ap

Example 3:

The following sample program is an example of using the string.Length and string.Insert() methods:

  1. using System;
  2. namespace CSharpExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string textString = "C# is one of the most popuplar programming language.";
  9. int length = textString.Length;
  10. //Insert "TutorialAce" at position 5 from the end of the string.
  11. string insertedString = " TutorialAce ";
  12. string newString = textString.Insert(length - 5, insertedString);
  13. Console.WriteLine("The new string: {0}", newString);
  14. }
  15. }
  16. }

Output:

The new string: C# is one of the most popuplar programming lang TutorialAce uage.

Example 4:

The following sample program is an example of using the string.Length and string.Substring() methods:

  1. namespace CSharpExample
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. string textString = "C# is one of the most popular programming language.";
  8. int length = textString.Length;
  9. //Obtains a 12-character substring starting at the 30th position from the end of the string.
  10. string substring = textString.Substring(length - 30, 12);
  11. Console.WriteLine("The returned substring: {0}", substring);
  12. }
  13. }
  14. }

Output:

The returned substring:  popular pro

Summary

In this tutorial, we discussed string length and gave examples of its use in string methods such as string.Remove(), string.Substring, and string.Insert().

Check out this page to learn about C#.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables