Skip to main content

Removes all leading and trailing blanks, converts tabs, newlines and carriage-returns into blanks, removes repeated blanks.

/// <summary>
/// Removes all leading and trailing blanks, converts tabs, newlines
/// and carriage-returns into blanks, removes repeated blanks.
/// </summary>
/// <param name="input">string to be trimmed.</param>
/// <returns>trimmed up string</returns>
public static string CleanWhiteSpace(string input)
{
    if (string.IsNullOrEmpty(input))
    {
        return input;
    }

    input = input.Replace('\n', ' ').Replace('\r', ' ').Replace('\t', ' ');

    while (-1 != input.IndexOf("  ", StringComparison.Ordinal))
    {
        input = input.Replace("  ", " ");
    }

    return input.Trim();
}