warrickj
warrickj

Reputation: 23

Split a string into substrings at variable widths in C#

I have a string of fixed length that has to be split at variable positions along the string to yield the substrings.

30849162 AUF3063100-2022031Doe Deanne 2610194031482100720081007200820000000000G43Z4206372 10 8 98282000000000911140000 00000000K6358Z8643K638 D126 Z099 320930090308009251519 132093 100720080071 0000000000000000000000000000000000000000000000000000000000000000000000002022031 000000000000000000000000000000000000000000000 00000000

The column break points are: 15, 18, 33, 61, 81, 89, 93, 94, 102, 110, 111, 114, 118,

Does anyone have an idea how I might do this? I have literally thousands of lines to parse

Upvotes: 1

Views: 582

Answers (3)

Magic Mick
Magic Mick

Reputation: 1523

Or you could use some nasty LINQ like so..

public string[] ReturnMyStrings(string str)
{
    int[] br = { 15, 18, 33, 61, 81, 89, 93, 94, 102, 110, 111, 114, 118 }; 
    return br.Select((x, i) => 
       str.Substring(br.ElementAtOrDefault(i - 1), x - br.ElementAtOrDefault(i - 1)))
       .ToArray();
}

Upvotes: 1

Matt Rowland
Matt Rowland

Reputation: 4595

If you wanted to make your code scaleable you could implement some classes to do this work.

static void Main(string[] args)
{
    string inputString = "30849162 AUF3063100-2022031Doe Deanne " +
                        "2610194031482100720081007200820000000000G43Z4" +
                        "206372 10 8 98282000000000911140000 00000000K" +
                        "6358Z8643K638 D126 Z099 320930090308009251519" +
                        "132093 100720080071 0000000000000000000000000" +
                        "000000000000000000000000000000000000000000000" +
                        "002022031 00000000000000000000000000000000000" +
                        "0000000000 00000000";

    //myRecord will hold the entire input in its split form
    var myRecord = new StringSplitterRecord()
    {
         fields = new List<StringSplitterField>()
         {
             //define all the different fields
             new StringSplitterField(inputString, 0, 15, "Name of field 1"),
             new StringSplitterField(inputString, 15, 3, "Name of field 2"),
             new StringSplitterField(inputString, 18, 15, "Name of field 3"),
             new StringSplitterField(inputString, 33, 28, "Name of field 4"),
             new StringSplitterField(inputString, 61, 20, "Name of field 5"),
             new StringSplitterField(inputString, 81, 8, "Name of field 6"),
             new StringSplitterField(inputString, 93, 1, "Name of field 7"),
             new StringSplitterField(inputString, 94, 8, "Name of field 8"),
             new StringSplitterField(inputString, 102, 8, "Name of field 9"),
             new StringSplitterField(inputString, 110, 1, "Name of field 10"),
             new StringSplitterField(inputString, 111, 3, "Name of field 11"),
             new StringSplitterField(inputString, 114, 4, "Name of field 12"),
        }
    };
}

class StringSplitterRecord
{
    public List<StringSplitterField> fields;
}

class StringSplitterField
{
    private string _contents;
    private string _fieldType;

    public StringSplitterField(string originalString, int startLocation, int length, string fieldType)
    {
        _contents = originalString.Substring(startLocation, length);
        _fieldType = fieldType;
    }
}

This will not only split your input string into the require pieces but it will put them all in a list with a name for each sub section. Then you can use LINQ etc to retrieve the data that you need.

Upvotes: 0

Krikor Ailanjian
Krikor Ailanjian

Reputation: 1852

Put the break points in an array and use .substring() in a loop through those numbers. This is roughly how you want to do it, though you will have to adjust it to compensate for exactly where you want your column breaks.

int[] nums = {0, 15, 18, 33, 61, 81, 89, 93, 94, 102, 110, 111, 114, 118 };
string input = "Long string here";

for (int i = 0; i < nums.Length - 1; i++)
{
    Console.WriteLine(input.Substring(nums[i], nums[i + 1] - nums[i]));
}

Upvotes: 2

Related Questions