Remove or replace a substring from a string in C#
This article illustrates the different techniques to remove or replace a substring from a string in C#.
The standard solution to remove a substring from a string is using the String.Remove() method. It returns a new string in which a specified number of characters from the string are deleted. The following sample illustrates the use of the Remove() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; public class Example { public static void Main() { String s = "ABCDEF"; int start = 3; int length = 2; s = s.Remove(start, length); Console.WriteLine(s); // ABCF } } |
The above code throws System.ArgumentOutOfRangeException if the length refers to a location outside the string. This can be handled by simply performing the length check.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; public class Example { public static void Main() { String s = "ABCDEF"; int start = 5; int length = 2; s = s.Remove(start, Math.Min(length, s.Length - start)); Console.WriteLine(s); // ABCDE } } |
In order to replace a portion of a string with another string, you can invoke the String.Insert() method on the resultant string, as shown 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 s = "ABCDEF"; int start = 5; int length = 2; string replacement = "XX"; s = s.Remove(start, Math.Min(length, s.Length - start)) .Insert(start, replacement); Console.WriteLine(s); // ABCDEXX } } |
It’s better to create a string extension method, ReplaceAt(), which replaces the characters in the current string beginning at the specified position, with the replacement 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 25 |
using System; public static class StringExtensions { public static string ReplaceAt(this string s, int start, int length, string replacement) { return s.Remove(start, Math.Min(length, s.Length - start)) .Insert(start, replacement); } } public class Example { public static void Main() { String s = "ABCDEF"; int start = 5; int length = 2; string replacement = "XX"; s = s.ReplaceAt(start, length, replacement); Console.WriteLine(s); // ABCDEXX } } |
Here’s an alternative method using the StringBuilder class. The benefit of using StringBuilder is that it considerably improves the code’s readability.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Text; public static class StringExtensions { public static string ReplaceAt(this string s, int start, int length, string replacement) { var sb = new StringBuilder(s); sb.Remove(start, Math.Min(length, s.Length - start)); sb.Insert(start, replacement); return sb.ToString(); } } public class Example { public static void Main() { String s = "ABCDEF"; int start = 5; int length = 2; string replacement = "XX"; s = s.ReplaceAt(start, length, replacement); Console.WriteLine(s); // ABCDEXX } } |
That’s all about removing or replacing a substring 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 :)