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().
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":
using System; namespace CSharpExample { class Program { static void Main(string[] args) { string textString = "C# is one of the most popuplar programming language. " + "With C#, you can develop mobile apps, desktop applicaitons, machine learning tools, " + "and web applicaitons"; //Get the length of the string. int length = textString.Length; //Write to the console. Console.WriteLine("The length of the string: {0}", length); } } }
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.
using System; namespace CSharpExample { class Program { static void Main(string[] args) { string textString = "C# is one of the most popuplar programming language. " + "With C#, you can develop mobile apps, desktop applicaitons, machine learning tools, " + "and web applicaitons"; int length = textString.Length; //Remove the last 10 characters string newString = textString.Remove(length - 10); Console.WriteLine("The new string: {0}", newString); } } }
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:
using System; namespace CSharpExample { class Program { static void Main(string[] args) { string textString = "C# is one of the most popuplar programming language."; int length = textString.Length; //Insert "TutorialAce" at position 5 from the end of the string. string insertedString = " TutorialAce "; string newString = textString.Insert(length - 5, insertedString); Console.WriteLine("The new string: {0}", newString); } } }
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:
namespace CSharpExample { class Program { static void Main(string[] args) { string textString = "C# is one of the most popular programming language."; int length = textString.Length; //Obtains a 12-character substring starting at the 30th position from the end of the string. string substring = textString.Substring(length - 30, 12); Console.WriteLine("The returned substring: {0}", substring); } } }
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#.