How To Remove Specific Characters From C# String

String.Remove() method removes a given number of characters from a string at a specified position. The position is a 0 index position. That means, the 0th position is the first character in the string.
 
In C#, Strings are immutable. That means, the method does not remove characters from a string. The method creates and returns a new string without those characters.
 
String.Remove() method has two overloaded forms.
 

Method

Description

Remove(Int32)

Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.

Remove(Int32, Int32)

Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.

 
Example 1
 
The following example removes all characters from a string that are after 25th position in the string.
  1. string founder = "Mahesh Chand is a founder of C# Corner";  
  2. // Remove all characters after first 25 chars  
  3. string first25 = founder.Remove(25);  
  4. Console.WriteLine(first25);  
The following example removes 12 characters from the 10th position in the string.
  1. // Remove characters start at 10th position, next 12 characters  
  2. String newStr = founder.Remove(10, 12);  
  3. Console.WriteLine(newStr);  
 
Example 2
 
Now, let’s say you want to delete everything after or before a substring in a string. We can use String.IndexOf() to find the position of the substring and can use starting index and number of characters to remove.
 
The following example removes everything before and after substring ‘founder’ in a string. 
  1. // Remove everything after founder  
  2. int pos = founder.IndexOf("founder");  
  3. if (pos >= 0)  
  4. {  
  5. // String after founder  
  6. string afterFounder = founder.Remove(pos);  
  7. Console.WriteLine(afterFounder);  
  8. // Remove everything before founder but include founder  
  9. string beforeFounder = founder.Remove(0, pos);  
  10. Console.Write(beforeFounder);  
  11. }  
The complete program is listed in Listing 1. 
  1. using System;  
  2. namespace RemoveStringSample  
  3. {  
  4. class Program  
  5. {  
  6. static void Main(string[] args)  
  7. {  
  8. /** Remove sample **/  
  9. string founder = "Mahesh Chand is a founder of C# Corner";  
  10. // Remove all characters after first 25 chars  
  11. string first25 = founder.Remove(25);  
  12. Console.WriteLine(first25);  
  13. // Remove characters start at 10th position, next 12 characters  
  14. String newStr = founder.Remove(10, 12);  
  15. Console.WriteLine(newStr);  
  16. int pos = founder.IndexOf("founder");  
  17. if (pos >= 0)  
  18. {  
  19. // String after founder  
  20. string afterFounder = founder.Remove(pos);  
  21. Console.WriteLine(afterFounder);  
  22. // Remove everything before founder but include founder  
  23. string beforeFounder = founder.Remove(0, pos);  
  24. Console.Write(beforeFounder);  
  25. }  
  26. Console.ReadKey();  
  27. }  
  28. }  
  29. }  
Listing 1.
 
Here is a detailed tutorial: Strings in C# 
 


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.