Remove 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
Updated on: 2020-06-22T15:14:34+05:30

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements