0

is there any way to join back the previous (looped line) in StringReader? because i would like to loop over a text to find a specific words, then join back with the previous line after found.

here what's i done so far:

using (StringReader reader = new StringReader(currentText))
 {
   string line;
   var prevline = "";
   var newline = "";
   var des= "";
   while ((line = reader.ReadLine()) != null)
   {
     string[] splittedTxt = line.Split(new[] { " " },
      StringSplitOptions.RemoveEmptyEntries);

      if (line.Contains("Fee"))
      {
        for (int i = 0; i < splittedTxt.Length; i++)
        {
         des += splittedTxt[i] + " ";
         newline = prevline +" " + des;
        }
     }
   } 
}
mkl
  • 90,588
  • 15
  • 125
  • 265
BBBBBBBB
  • 165
  • 1
  • 10
  • what do you want to achieve elaborate with some example – Always_a_learner Jun 23 '20 at 04:47
  • i actually creating a new datatable from a pdf, and i would like to find the text contains fee into excel, but after i use ```pdfReader```, the lines splitted into text and different line, so i want to join back the line after the found the words – BBBBBBBB Jun 23 '20 at 04:51
  • where is pdfreader in code? – Always_a_learner Jun 23 '20 at 04:53
  • that one should be under itextSharp i guess https://stackoverflow.com/questions/2550796/reading-pdf-content-with-itextsharp-dll-in-vb-net-or-c-sharp – BBBBBBBB Jun 23 '20 at 04:58
  • Share pdf reader code piece – Always_a_learner Jun 23 '20 at 04:59
  • Your specific problem is not iTextSharp related, so I removed the tag. Your `currentText` could have come from any source with multi-line text to analyze. – mkl Jun 23 '20 at 07:27
  • Does this answer your question? [How to read a text file reversely with iterator in C#](https://stackoverflow.com/questions/452902/how-to-read-a-text-file-reversely-with-iterator-in-c-sharp) – Liam Oct 08 '20 at 14:26

1 Answers1

2

StringReader is designed for reading a file forwards only.

The problem with strings is that you need to handle the encoding when reading them in order to store and display them properly, and the encoding are of different sizes.

Which means that you need to go a level lower and handle the stream and seek forwards and backwards and then determine and handle the encoding differences of the string.

See this for more details and a code example: How to read a text file reversely with iterator in C#

In your case it seems you just need the previous line. So reading backwards I think is not needed, you just need to store this for every iteration and then only use it if required. It seems like you already have most of what you need, you just need to store line in prevline like this:

while ((line = reader.ReadLine()) != null)
{
    string[] splittedTxt = line.Split(new[] { " " },
    StringSplitOptions.RemoveEmptyEntries);

    if (line.Contains("Fee"))
    {
        for (int i = 0; i < splittedTxt.Length; i++)
        {
           des += splittedTxt[i] + " ";
           newline = prevline +" " + des;
        }
    }
    prevline = line;
} 
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51