If i understand you well, you want to create custom "TagBuilder".
I'd suggest to create a class with
Parse
method.
Parse
method should do:
- split string into parts based on space,
- loop through the collection od parts to find "L" and "A" and its items,
- create and output string.
For example:
internal class TagBuilder
{
private string sIn = string.Empty;
private string sOut = string.Empty;
public TagBuilder(string _input)
{
sIn = _input;
}
public string Parse()
{
var parts = sIn.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries)
.Select((x,id) => new { Index = id, Item = x})
.ToList();
int i = 0;
int j = 0;
int c = parts.Count();
int il = 0;
int lc = 0;
StringBuilder sb = new StringBuilder();
do
{
var P = parts[i];
if(P.Item.Equals("01"))
{
il = P.Index;
lc = Convert.ToInt32(parts[il+1].Item);
sb.AppendLine($"Count of L items: {lc}");
j = 2;
}
if(P.Item.Equals("41"))
{
il = P.Index;
lc = Convert.ToInt32(parts[il+1].Item);
var aa = string.Join(",", parts.Skip(il+2).Take(lc).Select(o => o.Item));
sb.AppendLine($"\tCount of A items: {lc}: {{{aa}}}");
j = lc + 2;
}
i += j;
}
while(i<c);
sOut = sb.ToString();
return sOut;
}
public override string ToString()
{
sOut = Parse();
return sOut;
}
}
Usage:
string s = "01 04 41 03 78 79 7A 01 04 01 00 41 07 34 35 36 20 37 38 39 01 01 41 03 31 32 33 01 00 41 03 61 62 63 01 00";
TagBuilder tb = new TagBuilder(s);
tb.ToString().Dump();
Result:
Count of L items: 4
Count of A items: 3: {78,79,7A}
Count of L items: 4
Count of L items: 0
Count of A items: 7: {34,35,36,20,37,38,39}
Count of L items: 1
Count of A items: 3: {31,32,33}
Count of L items: 0
Count of A items: 3: {61,62,63}
Count of L items: 0
This is just an idea, not a complete solution. You have to change the code to your needs!