-2

How would I split and reverse files from a .txt file?

EDIT: Thank you.

Ended up working after: var SplitFileBySentence = Regex.Split(SplitFile, @"\.", RegexOptions.Multiline).OfType<string>().Reverse(); foreach (string Period in SplitFileBySentence) { richTextBox2.AppendText(Period); }

Omie
  • 5
  • 1
  • 9

1 Answers1

0

you are replacing the complete text of your second textbox for each line:

richTextBox2.Text = Period;

You should append the text instead of replacing everytime:

richTextBox2.AppendText(Period);
Sebastian Siemens
  • 2,302
  • 1
  • 17
  • 24
  • Oh, I see. So I replaced my richTextBox2.Text = Period; in my code with what you suggested, but it's still not outputting anything when I click split file. – Omie Apr 27 '16 at 07:00
  • Your regex is incorrect too. it should be `\.` instead of only `.`. look at the regex description of `.`. `var SplitFileBySentence = Regex.Split(SplitFile, @"\.", RegexOptions.Multiline).OfType().Reverse(); foreach (string Period in SplitFileBySentence) { richTextBox2.AppendText(Period); }` – Sebastian Siemens Apr 27 '16 at 07:10
  • Oh, thank you! My split file outputs correctly now with the last sentence of the text being at the top and so forth. I was also wondering, is there a way to add like a break between each sentence? Like it has in what the finished output should look like? – Omie Apr 27 '16 at 07:19
  • If you mean a linebreak you can use `Environment.NewLine`: `richTextBox2.AppendText(Period); richTextBox2.AppendText(Environment.NewLine);` – Sebastian Siemens Apr 27 '16 at 07:26
  • Ah that worked. Thanks for your help. Unfortunately, I think I completely misunderstood this exercise. I am supposed to split the file by each sentence first, and then actually use the stack.push method for each sentence and then pop it into the richTextBox2 and it should be reversed by its own. Would it be ok with you if I reworked my code using the stack.push method and got back to you on this if I run into a problem? – – Omie Apr 27 '16 at 07:45
  • you can edit your original question as there is already a description what you are looking for. Please post your current code in your original question. – Sebastian Siemens Apr 27 '16 at 08:29
  • Hey I just updated my new code in the original post. I have finished everything and everything is working fine, except I think I need to do Step 4 which is use the pop method to pop each line to my richTextBox2 window instead of using that foreach loop to append the text. How can I go about doing that? – Omie Apr 27 '16 at 08:55
  • `while (sentences.Count > 0) { string sentence = sentences.Pop(); richTextBox2.AppendText(sentence); richTextBox2.AppendText(Environment.NewLine); richTextBox2.AppendText(Environment.NewLine); }` Stack.Pop() will remove the element at the top of the stack. Because of this you have to check if there are elements in the stack by checking stack.count > 0. – Sebastian Siemens Apr 27 '16 at 09:51
  • Ah! Thank you so much! It's working now. I appreciate all your help. I updated my code again. So I think I am done with the exercise, just to make sure do you think my code satisfies all the steps of the exercise? – Omie Apr 27 '16 at 10:00
  • The only thing I am wondering about is that you push new strings in the stack instead of pushing the splitted lines from the file into the stack. The rest looks fine. – Sebastian Siemens Apr 27 '16 at 11:02
  • Oh ok. That's a good point. Since I already split the file in sentences in the first two lines of my Split File button click event (assuming my split is correct), do you know how I would go about pushing those splitted lines from the file into the stack instead of creating and pushing the new strings? – Omie Apr 27 '16 at 12:26
  • `SplitFileBySentence.ToList().ForEach(sentences.Push);` – Sebastian Siemens Apr 27 '16 at 13:07
  • Thank you very much! That worked. I just have one small problem, how would I be able to keep the periods at the end of each sentence when it outputs the split lines of the file to my richTextBox2? Currently, the periods which mark the end of each sentence are erased and I believe it is due to my string[] SplitFileBySentence = Regex.Split(SplitFile, @"\."); – Omie Apr 27 '16 at 18:44