C# Program to remove whitespaces in a string

In C#, there are several ways to remove whitespaces from a string. You can remove all spaces, only leading and trailing spaces, or all types of whitespace characters including tabs and newlines.

Using String.Replace() Method

The Replace()

string result = originalString.Replace(" ", "");

Example

using System;

class Demo {
   static void Main() {
      string str = "Patience is key!";
      Console.WriteLine("Original String: " + str);
      
      string result = str.Replace(" ", "");
      Console.WriteLine("After removing spaces: " + result);
   }
}

The output of the above code is −

Original String: Patience is key!
After removing spaces: Patienceiskey!

Using StringBuilder.Replace() Method

For mutable strings, you can use StringBuilder which modifies the string in-place −

Example

using System;
using System.Text;

class Demo {
   static void Main() {
      StringBuilder str = new StringBuilder("Patience is key!");
      Console.WriteLine("Original String: " + str.ToString());
      
      str.Replace(" ", "");
      Console.WriteLine("After removing spaces: " + str.ToString());
   }
}

The output of the above code is −

Original String: Patience is key!
After removing spaces: Patienceiskey!

Using String.Trim() Method

The Trim() method removes whitespace only from the beginning and end of a string −

Example

using System;

class Demo {
   static void Main() {
      string str = "  Patience is key!  ";
      Console.WriteLine("Original String: '" + str + "'");
      
      string result = str.Trim();
      Console.WriteLine("After trimming: '" + result + "'");
   }
}

The output of the above code is −

Original String: '  Patience is key!  '
After trimming: 'Patience is key!'

Removing All Whitespace Characters

To remove all types of whitespace including spaces, tabs, and newlines, you can use regular expressions −

Example

using System;
using System.Text.RegularExpressions;

class Demo {
   static void Main() {
      string str = "Patience\t is <br> key!";
      Console.WriteLine("Original String: " + str);
      
      string result = Regex.Replace(str, @"\s+", "");
      Console.WriteLine("After removing all whitespace: " + result);
   }
}

The output of the above code is −

Original String: Patience	 is 
 key!
After removing all whitespace: Patienceiskey!

Comparison of Methods

Method Purpose Performance
String.Replace() Remove specific characters Fast for simple replacements
StringBuilder.Replace() In-place modification Best for multiple operations
String.Trim() Remove leading/trailing whitespace Very fast
Regex.Replace() Remove all whitespace types Slower but more flexible

Conclusion

Use Replace() for removing specific characters like spaces, Trim() for leading/trailing whitespace, and Regex.Replace() for comprehensive whitespace removal. Choose StringBuilder when performing multiple string modifications for better performance.

Updated on: 2026-03-17T07:04:35+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements