public static string NormalizeMatrixString(string matStr) // From Andy - thank you! :)
{
// Remove any multiple spaces
while (matStr.IndexOf(" ") != -1)
matStr = matStr.Replace(" ", " ");
// Remove any spaces before or after newlines
matStr = matStr.Replace(" \r\n", "\r\n");
matStr = matStr.Replace("\r\n ", "\r\n");
// If the data ends in a newline, remove the trailing newline.
// Make it easier by first replacing \r\n’s with |’s then
// restore the |’s with \r\n’s
matStr = matStr.Replace("\r\n", "|");
while (matStr.LastIndexOf("|") == (matStr.Length - 1))
matStr = matStr.Substring(0, matStr.Length - 1);
matStr = matStr.Replace("|", "\r\n");
return matStr.Trim();
}