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.

Download  Run Code

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.

Download  Run Code

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.

Download  Run Code

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.

Download  Run Code

That’s all about removing a prefix from the start of a string in C#.