This post will discuss how to remove the first n characters from a string in C#.

Since strings are immutable in C#, the only feasible solution to removing first n characters from a string is to create a new string object with removed characters. There are several ways to do this:

1. Using String.Remove() method

The recommended solution to remove a range of characters from a string is to use its built-in Remove() method. This is demonstrated below:

Download  Run Code

2. Using String.Substring() method

We can use the String.Substring() method to retrieve a substring of a string. The following solution uses this method to create a new string that starts at a specified position and continues until the string’s end.

Download  Run Code

3. Using LINQ Skip() method

We can use LINQ Skip() method to bypass a specific number of elements in a string and return the remaining elements. We can either use this with the String.Join or String.Concat() method.

1. Using String.Join() method

Download  Run Code

2. Using String.Concat() method

Download  Run Code

4. Using range operator ..

Starting C# 8, we can use the range operator .., which specifies the start and end of a range as its operands. The following code example shows how to use this:

Download Code

5. Using String.ToCharArray() method

Here, the idea is to use the String.ToCharArray() method that takes a range and copies the characters in the specified range to a character array. The code might throw an ArgumentOutOfRangeException if a negative range is passed.

Download  Run Code

That’s all about removing first n characters from a string in C#.