Remove prefix from start of a string in C#
This article illustrates the different techniques to remove the prefix from the start of a string in C#.
1. Using String.Remove() method
The recommended solution is to use the String.Remove() method for removing a substring from the start of a string. It returns a new string in which a specified number of characters from the string are deleted. For example, the following code removes the first 5 characters from the given string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; public static class StringExtensions { public static string RemovePrefix(this string s, int prefixLen) { if (s.Length < prefixLen) { return string.Empty; } return s.Remove(0, prefixLen); } } public class Example { public static void Main() { String s = "HelloWorld"; s = s.RemovePrefix(5); Console.WriteLine(s); // World } } |
2. Using String.Substring() method
Alternatively, you can use the String.Substring() method for removing the substring from the start of a string. It returns a substring that starts at the specified character position till the end of the string. For example, the following code creates a substring starting at the 6th character to the end of the string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; public static class StringExtensions { public static string RemovePrefix(this string s, int prefixLen) { if (s.Length < prefixLen) { return string.Empty; } return s.Substring(prefixLen); } } public class Example { public static void Main() { String s = "HelloWorld"; s = s.RemovePrefix(5); Console.WriteLine(s); // World } } |
3. Using Range Operator
The above code can be shortened using the Range Operator as follows. The Range Operator can be used starting from C# 8, and is more elegant and concise.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; public static class StringExtensions { public static string RemovePrefix(this string s, int prefixLen) { return s[prefixLen..]; } } public class Example { public static void Main() { String s = "HelloWorld"; s = s.RemovePrefix(5); Console.WriteLine(s); // World } } |
4. Using Regex.Replace() method
Finally, you can use the Regex.Replace() method to replace substrings matching the specified regular expression pattern with the replacement string. The following code example demonstrates how to use the Regex.Replace() method from the System.Text.RegularExpressions namespace to remove the prefix from the beginning of a string. Here, the ^ anchor specifies the beginning of a string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Text.RegularExpressions; public static class StringExtensions { public static string RemovePrefix(this string s, string prefix) { return Regex.Replace(s, "^" + prefix, String.Empty); } } public class Example { public static void Main() { String s = "HelloWorld"; s = s.RemovePrefix("Hello"); Console.WriteLine(s); // World } } |
That’s all about removing a prefix from the start of 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 :)