Reputation: 1591
I'd appreciate if someone could advise on the following.
I read the file containing the below text and writing each line into the List<string>
:
CODE/1
NAME/some_name1
SHORT_NAME/short_name1
CODE/2
NAME/document is a piece of paper
containing information often
used as proof of something
SHORT_NAME/document is a piece
Now I'm parsing the list to get CODE, NAME and SHORT_NAME
separately.
The problem is that some lines containing NAME
have one sencence which is broken into several lines due to its long length. I want to append these lines into one sentence, the output should be:
...
NAME/document is a piece of paper containing information often used as proof of something
...
My code appends only one next line:
List<string> lines = File.ReadLines(path).ToList();
List<string> full_lines = new List<string>();
foreach (string line in lines)
{
if (line.StartsWith("NAME"))
{
name_index = lines.IndexOf(line);
string new_line = "";
if (!lines.ElementAt(name_index + 1).StartsWith("SHORT_NAME")) //checking if
//the next line does not start with SHORT_NAME (then it is continuation of NAME)
{
new_line = line + " " + lines.ElementAt(name_index + 1);//appending the next
//line
full_lines.Add(new_line); //adding into new list
}
else
{
full_lines.Add(line);
}
}
}
So the output is:
...
NAME/document is a piece of paper
...
So, how can I append all lines?
Thank you
Upvotes: 1
Views: 65
Reputation: 13902
change
if (!lines.ElementAt(name_index + 1).StartsWith("SHORT_NAME")) //checking if
//the next line does not start with SHORT_NAME (then it is continuation of NAME)
{
new_line = line + " " + lines.ElementAt(name_index + 1);//appending the next
//line
full_lines.Add(new_line); //adding into new list
}
else
{
full_lines.Add(line);
}
to
new_line = line;
name_index++;
while (!lines.ElementAt(name_index).StartsWith("SHORT_NAME"))
{
new_line = new_line + " " + lines.ElementAt(name_index);//appending the next line
name_index++;
}
full_lines.Add(new_line);
Upvotes: 0
Reputation: 6948
When you're reading the file, read each line separately, instead of all them together. Then don't create a new line unless it starts with a key word or if the '/' is unique unless the line contains a '/'. Something like this might help:
List<string> full_lines = new List<string>();
System.IO.StreamReader sr = new System.IO.StreamReader(path);
string line = "";
while(!sr.EndOfStream)
{
line = sr.ReadLine();
if(!line.Contains("/"))
{
full_lines[full_lines.Count - 1] += line;
}
else
full_lines.Add(line);
}
Upvotes: 1