How to remove the first character from a string in C#
In this tutorial, you will learn how to remove the first character from a string in C#.
We will show you how to remove the first character in a string using three different ways.
Removing the first character using the Replace method
To remove the first character using the Replace method; first, get the first character from the string and replace it with an empty string.
Here is an example:
using System; namespace CSharp_Remove_First_Character_From_String_Example { class Program { static void Main(string[] args) { string textString = "#abcdefghijklmnpq"; //Get the first character char firstCharacter = textString[0]; //Convert to a string string firstCharacterString = firstCharacter.ToString(); //Replace the first character with an empty string //string emptyString = ""; string emptyString = string.Empty; string newTextString = textString.Replace(firstCharacterString, emptyString); //Write to the console Console.WriteLine(newTextString); } } }
Output:
abcdefghijklmnpq
Learn more: String Length in C#
Removing the first character using the Remove method
You also can use the Remove method to remove the first character, as the following sample code shows:
using System; namespace CSharp_Remove_First_Character_From_String_Example { class Program { static void Main(string[] args) { string textString = "#abcdefghijklmnpq"; var newTextString = textString.Remove(0, 1); Console.WriteLine(newTextString); } } }
Output:
abcdefghijklmnpq
Removing the first character using the Substring method
It is also possible to remove the first character from a string using the C# Substring method.
Sample code:
using System; namespace CSharp_Remove_First_Character_From_String_Example { class Program { static void Main(string[] args) { string textString = "#abcdefghijklmnpq"; var newTextString = textString.Substring(1); Console.WriteLine(newTextString); } } }
Output:
abcdefghijklmnpq
Using TrimStart() to remove the first character
Sample code:
using System; namespace CSharp_Remove_First_Character_From_String_Example { class Program { static void Main(string[] args) { string textString = "#abcdefghijklmnpq"; var newTextString = textString.TrimStart(textString[0]); Console.WriteLine(newTextString); } } }
Summary
This tutorial showed three ways to remove the first character from a string.