reliably parsing data from string

-1

I've got a string of data that I am trying to parse so that they can be stored into a database. A sample of the data is shown below:

Finish@Store@000019411:02:48.499 1 0C 138D71802103D@000080211:03:16.530 1 0F 138D81802102B@000088211:04:10.546 1 0D 138D918021034@000023011:04:45.809 1 0F 138DA1802103B@000019411:04:47.202 1 08 238DB1802102D@000025111:05:51.550 1 0F 138DC18021037@000088111:05:53.429 1 0F 138DD18021048@11@$

Im trying to convert it to:

000019411:02:48.499 1 0C
000080211:03:16.530 1 0F

Im skipping the information before the @ symbol too. There is only an new line, carriage return at the very end of the string data. All the strings start with @Store@ and end with @$ The amount of records can vary greatly however so that can be an issue. Im trying to work our how to pull out the records relaibly as Ive been having mixed results.

if (data.Contains("Store@") == true)
                {
                    y = data.IndexOf("@Store@");
                   while (x<=data.Length) {
                        Console.WriteLine(x);

                        string substr = data.Substring(x,y);
                      string substr = data.Substring(y + 7, y + 18);
                        Console.WriteLine("Data: {0}", substr);
                       y = substr.Length+21;
                       x = y + 21;
                       substr = "";

}

The first time i run the code in debug mode with breakpoints I get the following outputs:

Data: 000019411:02:48.499 1 0C
66
Data: @000080211:03:16.530 1 0F 138D81802102B@000088211:04:10.546 1
105

Im not sure whats going on, the 66 and 105 is the current string length.

Another issue i have with this code is that when i run it i get a debug error:

System.ArgumentOutOfRangeException occurred HResult=0x80131502 Message=Index and length must refer to a location within the string. Parameter name: length

The program runs ok when i set breakpoints, the application which sends the data doesnt always send data, I have to tell it to and unfortunately i cant change the format of the data. Apologies for the long query. Thanks guys

c#
string
asked on Stack Overflow Mar 23, 2018 by cfcorp

1 Answer

0

Steve beat me to it with his comment, but assuming a constant record size:

const int recsize = 38;
const int outputsize = 24;
string data = "Finish@Store@000019411:02:48.499 1 0C 138D71802103D@000080211:03:16.530 1 0F 138D81802102B@000088211:04:10.546 1 0D 138D918021034@000023011:04:45.809 1 0F 138DA1802103B@000019411:04:47.202 1 08 238DB1802102D@000025111:05:51.550 1 0F 138DC18021037@000088111:05:53.429 1 0F 138DD18021048@11@$";
foreach(string s in data.Split("@"))
{
    if(s.Length == recsize)
        Console.WriteLine(s.Substring(0, outputsize));
}
Console.ReadKey();

Output:

000019411:02:48.499 1 0C
000080211:03:16.530 1 0F
000088211:04:10.546 1 0D
000023011:04:45.809 1 0F
000019411:04:47.202 1 08
000025111:05:51.550 1 0F
000088111:05:53.429 1 0F
answered on Stack Overflow Mar 23, 2018 by McGuireV10

User contributions licensed under CC BY-SA 3.0