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.
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(); } }
Patience is key! Patienceiskey!