C# Program to remove whitespaces in a string

ProgrammingServer Side ProgrammingCsharp

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!
raja
Published on 11-Sep-2018 12:02:33
Advertisements