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
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