using System; public class Test { public static string ReduceSpaces( string input ) { char[] a = input.ToCharArray(); int placeComma = 0, placeOther = 0; bool inQuotes = false; bool followedComma = true; foreach( char c in a ) { inQuotes ^= (c == '\"'); if (c == ' ') { if (!followedComma) a[placeOther++] = c; } else if (c == ',') { a[placeComma++] = c; placeOther = placeComma; followedComma = true; } else { a[placeOther++] = c; placeComma = placeOther; followedComma = false; } } return new String(a, 0, placeComma); } public static void Main() { string testcase = "1, 4, 2, \"PUBLIC, JOHN Q\" ,ACTIVE , 1332 , TROUBLE COLUMN "; System.Console.WriteLine("\"" + ReduceSpaces(testcase) + "\""); } }