using System.Text.RegularExpressions;
public static void Main()
string text = @"hi hiiiii
Console.Write(RemoveExcessiveWhitespace(text));
public static string RemoveExcessiveWhitespace(string value)
if (value == null) { return null; }
value = value.Trim().Replace("\r", " ");
value = value.Trim().Replace("\n", " ");
value = value.Trim().Replace(Environment.NewLine, " ");
var builder = new StringBuilder();
var ignoreWhitespace = false;
if (!ignoreWhitespace || c != ' ')
ignoreWhitespace = c == ' ';
return builder.ToString();