Click here to Skip to main content
16,022,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file as shown below.

NO: 1
ITEM: Device A
MESSAGE: This item was selected.
NO: 2
ITEM: Device B
MESSAGE: Exception: Could not find the item.

Exception thrown : 
System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

NO: 3
ITEM: Device C
MESSAGE: This item was selected.


In this program, I need to remove the info that not starts with the specific word and
make it in a group of 3. I stuck at here after remove the info that did not start with specific word. I try to use string.Join but I cant get the result that I want.

Final result should look like this:
NO: 1,ITEM: Device A,MESSAGE: This item was selected.
NO: 2,ITEM: Device B,MESSAGE: Exception: Could not find the item.
NO: 3,ITEM: Device C,MESSAGE: This item was selected.


What I have tried:

string[] lines = myInput.Split(new[] { "\n" }, StringSplitOptions.None);
for (int i = 0; i < lines.Count(); i++)
{                   
    if (lines[i].StartsWith("NO:") | lines[i].StartsWith("ITEM:") | lines[i].StartsWith("MESSAGE:"))
         {
             log.Info(lines[i]);             
            //NO: 1
            //ITEM: Device A
            //MESSAGE: This item was selected.
            //...
            //NO: 3
            //ITEM: Device C
            //MESSAGE: This item was selected.
             var r = lines[i].Split(new[] { "" }, StringSplitOptions.None);
             for (int m = 0; m < lines[i].Length; m += 3)
              {
                  var r = string.Join(" ", lines.Skip(m).Take(3));
                  log.Info(r);
              }         
          }
}

I have no idea how to continue with the line[i] and join it together as a group of 3. Please advise.
Posted
Updated 19-Jan-22 22:18pm
Comments
PIEBALDconsult 20-Jan-22 8:28am    
How is this significantly different from
https://www.codeproject.com/Questions/5322721/Csharp-merge-lines-of-strings-into-groups-of-3-at
?

I'd use a Regex:
C#
Regex reg = new Regex(@"(^NO:.*?\n)(.*?\n)(.*?\n)", RegexOptions.Multiline);
string input = File.ReadAllText(@"D:\Test data\Logging.dat");
MatchCollection matches = reg.Matches(input);
foreach (Match m in matches)
    {
    Console.WriteLine(m.Value.Replace("\r\n", " "));
    }
 
Share this answer
 
Try:
C#
string no = null, item = null, message = null;

string[] lines = myInput.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
    if (line.StartsWith("NO:"))
    {
        if (no != null)
        {
            log.LogInfo($"{no}, {item}, {message}");
            item = null;
            message = null;
        }
        
        no = line;
    }
    else if (line.StartsWith("ITEM:"))
    {
        item = line;
    }
    else if (line.StartsWith("MESSAGE:"))
    {
        message = line;
    }
}

if (no != null)
{
    log.LogInfo($"{no}, {item}, {message}");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900