0

Please someone to help me to parse these sample string below? I'm having difficulty to split the data and also the data need to add carriage return at the end of every event

sample string:

L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00

batch of events

expected output:

  • L,030216,182748,00,FF,I,00 - 1st Event
  • L,030216,182749,00,FF,I,00 - 2nd Event
  • L,030216,182750,00,FF,I,00 - 3rd Event
semirturgay
  • 4,151
  • 3
  • 30
  • 50
Miggy
  • 31
  • 3
  • 2
    What determines how you split the string and where? – Prescott Feb 04 '16 at 05:49
  • Is the length of the line variable? Or is each line always a batch of three events? – marco Feb 04 '16 at 05:53
  • this will be the string format - L,030216,182748,00,FF,I,00 w/ substring of 6 – Miggy Feb 04 '16 at 05:54
  • @marco - It depends sometimes its more longer – Miggy Feb 04 '16 at 05:55
  • But where are you meant to be splitting? Does each `event` always start with L? Could there be an L inside the string? – Corey Thompson Feb 04 '16 at 05:55
  • Will [a regex `\d+,\d+,00,FF,I,00`](http://regexstorm.net/tester?p=%5cd%2b%2c%5cd%2b%2c00%2cFF%2cI%2c00&i=L%2c030216%2c182748%2c00%2cFF%2cI%2c00%2c030216%2c182749%2c00%2cFF%2cI%2c00%2c030216%2c182750%2c00%2cFF%2cI%2c00) help? (see table tab for matches) – Sebastian Schumann Feb 04 '16 at 05:58
  • @Corey L - only starts with first character/line of string when parsing need to add also L – Miggy Feb 04 '16 at 05:58

6 Answers6

1

This should do the trick, given there are no more L's inside your string, and the comma place is always the sixth starting from the beginning of the batch number.

class Program
    {
        static void Main(string[] args)
        {


            String batchOfevents = "L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00,030216,182751,00,FF,I,00,030216,182752,00,FF,I,00,030216,182753,00,FF,I,00";

            // take out the "L," to start processing by finding the index of the correct comma to slice. 
            batchOfevents = batchOfevents.Substring(2);

            String output = "";
            int index = 0;
            int counter = 0;
            while (GetNthIndex(batchOfevents, ',', 6) != -1)
            {

            counter++;

            if (counter == 1){
                index = GetNthIndex(batchOfevents, ',', 6);
                output += "L, " + batchOfevents.Substring(0, index) + " - 1st event\n";
                batchOfevents = batchOfevents.Substring(index + 1);

            } else if (counter == 2) {
                index = GetNthIndex(batchOfevents, ',', 6);
                output += "L, " + batchOfevents.Substring(0, index) + " - 2nd event\n";
                batchOfevents = batchOfevents.Substring(index + 1);

            }
            else if (counter == 3)
            {
                index = GetNthIndex(batchOfevents, ',', 6);
                output += "L, " + batchOfevents.Substring(0, index) + " - 3rd event\n";
                batchOfevents = batchOfevents.Substring(index + 1);

            } else {

                index = GetNthIndex(batchOfevents, ',', 6);
                output += "L, " + batchOfevents.Substring(0, index) + " - " + counter + "th event\n";
                batchOfevents = batchOfevents.Substring(index + 1);

            }


            }


            output += "L, " + batchOfevents + " - " + (counter+1) + "th event\n";


            Console.WriteLine(output);
        }

        public static int GetNthIndex(string s, char t, int n)
        {
            int count = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == t)
                {
                    count++;
                    if (count == n)
                    {
                        return i;
                    }
                }
            }
            return -1;
        }
    }

Now the output will be in the format you asked for, and the original string has been decomposed.

NOTE: the getNthIndex method was taken from this old post.

Community
  • 1
  • 1
A-Ace
  • 51
  • 2
1

Seems like an easy problem. Something as easy as this should do it:

    string line = "L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00";
    string[] array = line.Split(',');
    StringBuilder sb = new StringBuilder();
    for(int i=0; i<array.Length-1;i+=6)
    {
        sb.AppendLine(string.Format("{0},{1} - {2} event",array[0],string.Join(",",array.Skip(i+1).Take(6)), "number"));
    }

output (sb.ToString()):

L,030216,182748,00,FF,I,00 - number event
L,030216,182749,00,FF,I,00 - number event
L,030216,182750,00,FF,I,00 - number event

All you have to do is work on the function that increments the ordinals (1st, 2nd, etc), but that's easy to get.

Community
  • 1
  • 1
Tyress
  • 3,573
  • 2
  • 22
  • 45
0

If you want to split the string into multiple strings, you need a set of rules, which are implementable. In your case i would start splitting the complete string by the given comma , and than go though the elements in a loop. All the strings in the loop will be appended in a StringBuilder. If your ruleset say you need a new line, just add it via yourBuilder.Append('\r\n') or use AppendLine.

EDIT

Using this method, you can also easily add new chars like L or at the end rd Event

BendEg
  • 20,098
  • 17
  • 57
  • 131
0
  1. Look for the start index of 00,FF,I,00 in the entire string.
  2. Extract a sub string starting at 0 and index plus 10 which is the length of the characters in 1.
  3. Loop through it again each time with a new start index where you left of in 2.
  4. Add a new line character each time.
Vivek Verma
  • 333
  • 3
  • 13
0

Have a try the following:

string stream = "L,030216,182748,00,FF,I,00,  030216,182749,00,FF,I,00,  030216,182750,00,FF,I,00";
string[] lines = SplitLines(stream, "L", "I", ",");

Here the SplitLines function is implemented to detect variable-length events within the arbitrary-formatted stream:

string stream = "A;030216;182748 ;00;FF;AA;01; 030216;182749;AA;02";
string[] lines = SplitLines(batch, "A", "AA", ";");

Split-rules are:
- all elements of input stream are separated by separator(, for example).
- each event is bounded by the special markers(L and I for example)
- end marker is previous element of event-sequence

static string[] SplitLines(string stream, string startSeq, string endLine, string separator) {
    string[] elements = stream.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);
    int pos = 0;
    List<string> line = new List<string>();
    List<string> lines = new List<string>();
    State state = State.SeqStart;
    while(pos < elements.Length) {
        string current = elements[pos].Trim();
        switch(state) {
            case State.SeqStart:
                if(current == startSeq)
                    state = State.LineStart;
                continue;
            case State.LineStart:
                if(++pos < elements.Length) {
                    line.Add(startSeq);
                    state = State.Line;
                }
                continue;
            case State.Line:
                if(current == endLine)
                    state = State.LineEnd;
                else
                    line.Add(current);
                pos++;
                continue;
            case State.LineEnd:
                line.Add(endLine);
                line.Add(current);
                lines.Add(string.Join(separator, line));
                line.Clear();
                state = State.LineStart;
                continue;
        }
    }
    return lines.ToArray();
}
enum State { SeqStart, LineStart, Line, LineEnd };
DmitryG
  • 17,677
  • 1
  • 30
  • 53
0

f you want to split the string into multiple strings, you need a set of rules, which are implementable. In your case i would start splitting the complete string by the given comma , and than go though the elements in a loop. All the strings in the loop will be appended in a StringBuilder. If your ruleset say you need a new line, just add it via yourBuilder.Append('\r\n') or use AppendLine.

Rakesh
  • 313
  • 1
  • 3
  • 14