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.
using System; namespace CSharp_Remove_Last_Character_From_String_Example { class Program { static void Main(string[] args) { string textString = "The Best C# Programming Language Tutorials!#"; int indexOftheLastCharacter = textString.Length - 1; var newTextString = textString.Remove(indexOftheLastCharacter); Console.WriteLine(newTextString); } } }
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.
using System; namespace CSharp_Remove_Last_Character_From_String_Example { class Program { static void Main(string[] args) { string textString = "The Best C# Programming Language Tutorials!#"; char lastCharacter = textString[textString.Length - 1]; var newTextString = textString.Replace(lastCharacter.ToString(), string.Empty); Console.WriteLine(newTextString); } } }
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.
using System; namespace CSharp_Remove_Last_Character_From_String_Example { class Program { static void Main(string[] args) { string textString = "The Best C# Programming Language Tutorials!#"; var newTextString = textString.Substring(0, textString.Length - 1); Console.WriteLine(newTextString); } } }
Output:
The Best C# Programming Language Tutorials!
Summary
This tutorial introduced various ways to remove the last character from a string in C#.