public static class Program
public static string RemoveWhitespace(this string str)
return string.Join(" ", str.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
public static void Main()
var strWithSpace = "hello i \n 222 \t tab me \n !. Cow \r\n moooo";
var strNoWhiteSpace = System.Text.RegularExpressions.Regex.Replace(strWithSpace, @"\s+", " ");
Console.WriteLine($"Original string: {strWithSpace}" );
Console.WriteLine($"Use Regex: {strNoWhiteSpace}" );
Console.WriteLine($"Use Extension: {strWithSpace.RemoveWhitespace()}" );
Console.WriteLine($"Use Replace: {strWithSpace.Replace(" ","")}" );