C# Remove Last Character from String

How to remove the last character from a string in C#

This tutorial explains how to remove the last character from a string in C#.

Related tutorials:

How to remove the last character from a string using Remove()

The Remove method can be used to remove the last character from a string, as shown in the sample code below.

  1. using System;
  2. namespace CSharp_Remove_Last_Character_From_String_Example
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string textString = "The Best C# Programming Language Tutorials!#";
  9. int indexOftheLastCharacter = textString.Length - 1;
  10. var newTextString = textString.Remove(indexOftheLastCharacter);
  11. Console.WriteLine(newTextString);
  12. }
  13. }
  14. }

Output:

The Best C# Programming Language Tutorials!

How to delete the last character from a string with Replace()

You can also use the Replace method to delete the last character, as shown in the sample code below.

  1. using System;
  2. namespace CSharp_Remove_Last_Character_From_String_Example
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string textString = "The Best C# Programming Language Tutorials!#";
  9. char lastCharacter = textString[textString.Length - 1];
  10. var newTextString = textString.Replace(lastCharacter.ToString(), string.Empty);
  11. Console.WriteLine(newTextString);
  12. }
  13. }
  14. }

Output:

The Best C Programming Language Tutorials!

How to remove the last character from a string using Substring()

The Substring method can be used to remove the last character, and an example is shown below.

  1. using System;
  2. namespace CSharp_Remove_Last_Character_From_String_Example
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string textString = "The Best C# Programming Language Tutorials!#";
  9. var newTextString = textString.Substring(0, textString.Length - 1);
  10. Console.WriteLine(newTextString);
  11. }
  12. }
  13. }

Output:

The Best C# Programming Language Tutorials!

Summary

This tutorial introduced various ways to remove the last character from a string in C#.


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