Remove first n characters from a string in C#
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { string str = "Techie Delight"; int n = 7; str = str.Remove(0, n); Console.WriteLine(str); } } /* Output: Delight */ |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { string str = "Techie Delight"; int n = 7; str = str.Substring(n); Console.WriteLine(str); } } /* Output: Delight */ |
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Linq; public class Example { public static void Main() { string str = "Techie Delight"; int n = 7; str = String.Join(String.Empty, str.Skip(n)); Console.WriteLine(str); } } /* Output: Delight */ |
2. Using String.Concat() method
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Linq; public class Example { public static void Main() { string str = "Techie Delight"; int n = 7; str = String.Concat(str.Skip(n)); Console.WriteLine(str); } } /* Output: Delight */ |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { string str = "Techie Delight"; int n = 7; str = str[n..]; Console.WriteLine(str); } } /* Output: Delight */ |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { string str = "Techie Delight"; int n = 7; str = new string(str.ToCharArray(n, str.Length - n)); Console.WriteLine(str); } } /* Output: Delight */ |
That’s all about removing first n characters from a string in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)