C# Program to remove whitespaces in a string


Let’s say the following is the string −

StringBuilder str = new StringBuilder("Patience is key!");

To remove whitespace, you can use the replace method.

str.Replace(" ", "");

Let us see the complete code.

Example

 Live Demo

using System;
using System.Text;
class Demo {
   static void Main() {
      // Initial String
      StringBuilder str = new StringBuilder("Patience is key!");
      Console.WriteLine(str.ToString());

      // Replace
      str.Replace(" ", "");

      // New String
      Console.WriteLine(str.ToString());
      Console.ReadLine();
   }
}

Output

Patience is key!
Patienceiskey!

Updated on: 22-Jun-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements