Consider using StringBuilder
To speed things up, you might have used a StringBuilder instead of a string for output. Quicker to add to than appending to the end of a string. Then for the 2nd (foreach) loop, get the string from stringbuilder then proceed.
Keep Up with TechRepublic