'Trying to replace all white space with a single space

My program is a file validation utility. I have to read in a format file and then parse out each line by a single space. But obviously, the person who wrote the format file may use tabs, or two spaces, or any form of whitespace, and I'm looking for some code to do that. I've tried this:

public static string RemoveWhitespace(this string line)
{
    try
    {
        return new Regex(@"\s*").Replace(line, " ");
    }
    catch (Exception)
    {
        return line;
    }
}

I'm assuming this is wrong. What should I do?



Solution 1:[1]

input = input.Replace("\t", " ");

List<string> empties = new List<string>();
for (int i=input.Length - 1; i>1; i--)
{
    string spcs = "";
    for (int j=0; j<=i; j++)
        spcs += " ";
    if (input.Contains(spcs))
        empties.Add(spcs);
}

foreach (string s in empties)
    input = input.Replace(s, " ");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1