Click here to Skip to main content
15,565,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file which look like this:
Name:John
Age:18
Info:(text message)
Name:May
Age:17
Info:(text message)
Name:Jane
Age:17
Info:(text message)


I want to make Name, Age, Info in 1 group. Thus, I make the string like this by the code below,
Name:John,Age:18,Info:(text message)
Name:May,Age:17,Info:(text message)
Name:Jane,Age:17,Info:(text message)


However, the sequence will get disturb if the 'info' part have a long message and go into second line.
<pre>Name:John
Age:18
Info:(text message)
Name:May
Age:17
Info:(text................................
....................................message)
Name:Jane
Age:17
Info:(text message)


Any suggestion if I want to group them with specified word(Name, Age, Info)? So that it would not get disturb when the text message is long.

What I have tried:

string[] lines = myInputString.Split(new[] { "\r\n" }, StringSplitOptions.None);
for (int i = 0; i < lines.Count(); i = i + 3)
{
    var res = string.Join(",", lines.Skip(i).Take(3));

}
Posted
Updated 16-Jan-22 23:32pm
v3
Comments
BillWoodruff 17-Jan-22 0:36am    
Name:John,Age:18,Hobby:(text message)
Name:May,Age:17,Hobby:(text message)
Name:Jane,Age:17,Hobby:(text message)

this is the string output you want ? where each "text message" can havew any number of lines ?

For a console application:
var lines = File.ReadAllLines(fileName);

foreach (var line in lines)
{
    if (line.StartsWith("Name:"))
    {
        Console.WriteLine();
    }
    else
    {
        Console.Write(", ");
    }

    Console.Write(line);
}
 
Share this answer
 
v2
Comments
Maciej Los 17-Jan-22 11:48am    
5ed!
George Swan 17-Jan-22 18:08pm    
Brilliant 5ed!
Try the following code:

string[] lines = myInputString.Split(new[] { "\r\n" }, StringSplitOptions.None);
var rgex = new System.Text.RegularExpressions.Regex("(Name|Age|Info)");
for (int i = 0; i < lines.Count(); i++)
{
        if (rgex.Match(lines[i]).Success)
        {
                result = result + lines[i] + " ";
                // Add New Line after all concat
                if (lines[i].Contains(")"))
                        result = result + "\n";
        }
        else
        {
                result = result.TrimEnd();
                        result = result + lines[i] + " ";
                // Add New Line after all concat
                if (lines[i].Contains(")"))
                        result = result + "\n";
        }
}


Output:
Quote:
Name:John Age:18 Info:(text message)
Name:May Age:17 Info:(text....................................................................message)
Name:Jane Age:17 Info:(text message)
 
Share this answer
 
Comments
lmoelleb 17-Jan-22 3:20am    
Your regex will trigger if for example "Age" is somewhere in the text. For example if the hobby is "Bronze Age history". I suggest changing it to check for the beginning of the line and being followed by a colon. That said, Regex is also somewhat complicate for beginners, so String.StartsWith might be a better option even though the code will be a bit longer.
Maciej Los 17-Jan-22 3:47am    
Good point!
My virtual 5!

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