public static string CleanWhitespaces(this string text)
{
var sb = new StringBuilder();
bool whitespace = false;
foreach (char c in text.Trim())
{
if (!Char.IsWhiteSpace(c))
{
whitespace = false;
sb.Append(c);
continue;
}
if (whitespace)
continue;
whitespace = true;
sb.Append(' ');
}
return sb.ToString();
}