Removing whitespaces using C# String.Empty


Set the string.

StringBuilder str = new StringBuilder("Tom Hanks");

Now, use the replace() method to replace whitespace with String. Empty. This will eventually remove the whitespace.

str.Replace(" ", String.Empty);

Let us see the complete code −

Example

 Live Demo

using System;
using System.Text;
class Demo {
   static void Main() {
      StringBuilder str = new StringBuilder("Tom Hanks");
      Console.WriteLine(str.ToString());
      str.Replace(" ", String.Empty);
      Console.WriteLine(str.ToString());
      Console.ReadLine();
   }
}

Output

Tom Hanks
TomHanks

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

49 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements