Q:

Complex Concatenation Of Two Headings

If I have the following headings in a .txt file:
     CUSTOMER             PAYMENT       ERR CORR    PAYMENT AMOUNT     PAYMENT  
  REFERENCE NUMBER    INSTRUCTION TYPE   REASON*                        TYPE**

How can I create a List<string> with the following elements:
CUSTOMER REFERENCE NUMBER, PAYMENT INSTRUCTION TYPE, ERR CORR REASON*,   PAYMENT AMOUNT, PAYMENT TYPE** 
Another example:
     CUSTOMER             PAYMENT       ERR CORR    PAYMENT AMOUNT     PAYMENT  
  REFERENCE NUMBER    INSTRUCTION TYPE                                  TYPE**

would give me:
 CUSTOMER REFERENCE NUMBER, PAYMENT INSTRUCTION TYPE, ERR CORR,   PAYMENT AMOUNT, PAYMENT TYPE** 
Note that headings are spaced by a number of white spaces >= 2 while each word in the one heading is separated by one white space. It's possible to have headings written only on the first row.

A:

Here's one way. First, "merge" the two lines together. I am assuming they are the same length. Example:
var s1 = "    CUSTOMER             PAYMENT       ERR CORR    PAYMENT AMOUNT     PAYMENT  ";
var s2 = " REFERENCE NUMBER    INSTRUCTION TYPE   REASON*                        TYPE**  ";

var merged = new string(Enumerable.Range(0, s1.Length)
    .Select(t=>s1[t]!=' ' ? s1[t] : s2[t]).ToArray());

This gets you something like:
 REFCUSTOMERUMBER    INSTPAYMENT TYPE  ERRACORR    PAYMENT AMOUNT     PAYMENT  

Then, look for the indexes of double (or more) spaces in that string:
var indexes = Regex.Matches(merged, @"  +").OfType<Match>().Select(t=>t.Index).ToList();

Then, use those indexes to cut both strings up and concatenate them, trimming off the whitespace.   I explicitly add the start and end of the string, in case there is no double whitespace on the ends.
indexes.Insert(0, 0);
indexes.Add(merged.Length-1);

indexes.Sort(); // not sure if Regex.Matches will necessarily be in order

var columns = new List<string>();
for (int i=0; i<indexes.Count-1;i++) 
{
    var column = (
        s1.Substring(indexes[i], indexes[i+1]-indexes[i]).Trim() + " " + 
        s2.Substring(indexes[i], indexes[i+1]-indexes[i]).Trim()
        ).Trim();
    if (column != string.Empty) 
        columns.Add(column);
}

This gets you the output:
CUSTOMER REFERENCE NUMBER 
PAYMENT INSTRUCTION TYPE 
ERR CORR REASON* 
PAYMENT AMOUNT 
PAYMENT TYPE** 

And here is a working example.

