C# – Remove first or last character from a string

Use string.Remove() to remove character(s) from a string based on their index, such as the first or last character. This method has two parameters:

  • startIndex: The position in the string to start removing characters.
  • count: How many characters to remove.

string.Remove() returns a new string with the characters removed. I’ll show examples below.

Remove the first character from a string

To remove the first character, use string.Remove(startIndex: 0, count: 1), like this:

string input = "hello";

string cleanedInput = input.Remove(startIndex: 0, count: 1);

Console.WriteLine($"Before: {input}");
Console.WriteLine($"After: {cleanedInput}");
Code language: C# (cs)

This outputs the following:

Before: hello
After: elloCode language: plaintext (plaintext)

Example: Remove the first 3 characters from a string

To remove the first N characters, set count to however how many characters you want to remove. For example, to remove the first 3 characters, use string.Remove(startIndex: 0, count: 3), like this:

string input = "hello";

string cleanedInput = input.Remove(startIndex: 0, count: 3);

Console.WriteLine($"Before: {input}");
Console.WriteLine($"After: {cleanedInput}");
Code language: C# (cs)

This outputs the following:

Before: hello
After: loCode language: plaintext (plaintext)

Remove the last character from a string

The last character is at index Length – 1. Therefore, to remove the last character, use string.Remove(input.Length – 1), like this:

string input = "world";

string cleanedInput = input.Remove(startIndex: input.Length - 1);

Console.WriteLine($"Before: {input}");
Console.WriteLine($"After: {cleanedInput}");
Code language: C# (cs)

This outputs the following:

Before: world
After: worlCode language: plaintext (plaintext)

Example: Remove last 3 characters from a string

Notice that count is optional. If you don’t specify count, it’ll remove all characters starting at the specified index. So to remove the last N characters, use string.Remove(Length – N). Here’s an example of removing the last 3 characters:

string input = "world";

string cleanedInput = input.Remove(startIndex: input.Length - 3);

Console.WriteLine($"Before: {input}");
Console.WriteLine($"After: {cleanedInput}");
Code language: C# (cs)

This outputs:

Before: world
After: woCode language: plaintext (plaintext)

Example: Remove the second to last character from a string

You can remove a character from anywhere in the string by specifying its index and count=1. The last character is at index Length – 1, so the second to last character is at index Length – 2. Therefore, to remove the second to last character, use input.Remove(input.Length – 2, count: 1), like this:

string input = "hello worl@d";

string cleanedInput = input.Remove(input.Length - 2, count: 1);

Console.WriteLine($"Before: [{input}]");
Console.WriteLine($"After: [{cleanedInput}]");
Code language: C# (cs)

This outputs the following:

Before: [hello worl@d]
After: [hello world]Code language: plaintext (plaintext)

Remove first and last character from a string

When you want to remove the first and last character at the same time, use string.Substring(). This is simpler than using multiple string.Remove() calls. With Substring(), you specify the starting index and the substring length. To get a substring excluding the first and last characters, use Substring(1, Length – 2), like this:

string input = "hello";

var cleanedInput = input.Substring(startIndex: 1, input.Length - 2);

Console.WriteLine($"Before: {input}");
Console.WriteLine($"After: {cleanedInput}");
Code language: C# (cs)

This outputs the following:

Before: hello
After: ellCode language: plaintext (plaintext)

Leave a Comment