1

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

6
  • Forgot: The original string is always 1257 characters long Commented Jul 29, 2015 at 1:32
  • What do you mean by variable? Can the break be at any of the points you have listed, or at each of them? Commented Jul 29, 2015 at 1:33
  • 2
    Can't you just take the substrings? 0-15, 16-18, 19-33 etc? Commented Jul 29, 2015 at 1:35
  • Check out my answer here stackoverflow.com/questions/29653991/…. This should help you. Commented Jul 29, 2015 at 1:39
  • Because you know the length of each field, here's a fiddle demo (dotnetfiddle.net/mZNIEW) with the code from my previous comment. Commented Jul 29, 2015 at 2:37

3 Answers 3

2

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]));
}
Sign up to request clarification or add additional context in comments.

Comments

1

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();
}

Comments

0

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.

3 Comments

Thanks Matt. I'm thinking I could combine your method and Shar1er80 to scale it
The best option for code scalability is a custom FixedLengthAttribute. For example, a model can have several string properties, and each of it has a fixed length. You can easily map this model to any string by specifying field delimiter.
@AndreyMolotkov if you were to make this an attribute then you would have to hard code the length into the class/attribute. By making the length a parameter in the constructor it enables a dynamic setting of the length. This give the code more reusability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.