[Solved] How to split a string by white spaces in C# [closed]


The data is fixed width columns so use following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            FIX_WIDTH fixWidth = new FIX_WIDTH(FILENAME);
        }
    }
    public class FIX_WIDTH
    {
        int[] Start_Position = { 0, 55, 105, 135, 155, 176, 207, 215, 219, 223, 243, 264};
        string[] Column_Names = { "Col_A", "Col_B", "Col_C", "Col_D", "Col_E", "Col_F", "Col_G", "Col_H", "Col_I", "Col_J", "Col_K", "Col_L" };
        public DataTable dt;
        public FIX_WIDTH(string filename)
        {
            dt = new DataTable();
            foreach (string col in Column_Names)
            {
                dt.Columns.Add(col, typeof(string));
            }

            StreamReader reader = new StreamReader(filename);
            string line = "";
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.Length > 0)
                {
                    List<string> row = new List<string>();
                    for (int i = 0; i < Start_Position.Length; i++ )
                    {
                        int pos = Start_Position[i];
                        if (pos >= line.Length)
                        {
                            break;
                        }
                        else
                        {
                            if (i == Start_Position.Length - 1)
                            {
                                row.Add(line.Substring(pos).Trim());
                            }
                            else
                            {
                                int length = Start_Position[i + 1] - pos;
                                if (pos + length <= line.Length)
                                {
                                    row.Add(line.Substring(pos, length).Trim());
                                }
                                else
                                {
                                    row.Add(line.Substring(pos).Trim());
                                }
                            }
                        }
                    }
                    dt.Rows.Add(row.ToArray());
                
                }
            }

        }

    }
}

solved How to split a string by white spaces in C# [closed]