C# Remove First Character from String

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:

  1. using System;
  2. namespace CSharp_Remove_First_Character_From_String_Example
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string textString = "#abcdefghijklmnpq";
  9. //Get the first character
  10. char firstCharacter = textString[0];
  11. //Convert to a string
  12. string firstCharacterString = firstCharacter.ToString();
  13. //Replace the first character with an empty string
  14. //string emptyString = "";
  15. string emptyString = string.Empty;
  16. string newTextString = textString.Replace(firstCharacterString, emptyString);
  17. //Write to the console
  18. Console.WriteLine(newTextString);
  19. }
  20. }
  21. }

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:

  1. using System;
  2. namespace CSharp_Remove_First_Character_From_String_Example
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string textString = "#abcdefghijklmnpq";
  9. var newTextString = textString.Remove(0, 1);
  10. Console.WriteLine(newTextString);
  11. }
  12. }
  13. }

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:

  1. using System;
  2. namespace CSharp_Remove_First_Character_From_String_Example
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string textString = "#abcdefghijklmnpq";
  9. var newTextString = textString.Substring(1);
  10. Console.WriteLine(newTextString);
  11. }
  12. }
  13. }

Output:

abcdefghijklmnpq

Using TrimStart() to remove the first character

Sample code:

  1. using System;
  2. namespace CSharp_Remove_First_Character_From_String_Example
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string textString = "#abcdefghijklmnpq";
  9. var newTextString = textString.TrimStart(textString[0]);
  10. Console.WriteLine(newTextString);
  11. }
  12. }
  13. }

Summary

This tutorial showed three ways to remove the first character from a string.


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