Hi guys I really need your help. I was trying to take a multi-line string which was concluded of a few paragraphs and split it into a few individual texts.
I realized that whenever I skip a line there is a sequence of \n\r in there. Afterwards I thought that each new line starts with a \n and end with a \r. Therefor, I wrote the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication15
{
class Program
{
struct ParagraphInfo
{
public ParagraphInfo(string text)
{
int i;
Text = text;
i = text.IndexOf('.');
FirstSentence = text.Substring(0, i);
}
public string Text, FirstSentence;
}
static void Main(string[] args)
{
int tmp = 0;
int tmp1 = 0;
string MultiParagraphString = @"AA.aa.
BB.bb.
CC.cc.
DD.dd.
EE.ee.";
List<ParagraphInfo> Paragraphs = new List<ParagraphInfo>();
Regex NewParagraphFinder = new Regex(@"[\n][\r]");
MatchCollection NewParagraphMatches = NewParagraphFinder.Matches(MultiParagraphString);
for (int i = 0; i < NewParagraphMatches.Count; i++)
{
if (i == 0)
{
Paragraphs.Add(new ParagraphInfo((MultiParagraphString.Substring(0, NewParagraphMatches[0].Index))));
}
else if (i == (NewParagraphMatches.Count - 1))
{
tmp = NewParagraphMatches[i].Index + 3;
tmp1 = MultiParagraphString.Length - NewParagraphMatches[i].Index - 3;
Paragraphs.Add(new ParagraphInfo(MultiParagraphString.Substring(tmp, tmp1)));
}
else
{
tmp = NewParagraphMatches[i].Index + 3;
tmp1 = NewParagraphMatches[i + 1].Index - NewParagraphMatches[i].Index+3;
Paragraphs.Add(new ParagraphInfo(MultiParagraphString.Substring(tmp, tmp1)));
}
}
Console.WriteLine(MultiParagraphString);
foreach (ParagraphInfo Paragraph in Paragraphs)
{
Console.WriteLine(Paragraph.Text);
}
}
}
}
when I printed each member of Paragraphs one after another alongside the entire text something rather bizarre came appeared. The output of the Paragraph list was this:
AA.aa.
CC.cc.
DD.
DD.dd.
EE.
EE.ee.
I can not understand why does this keep happening, and moreover I can not figure out why is the output so different each time.
Sorry if it's a mess but I really need some help here. BTW if anyone has a better idea to do it feel free to share..
Thanks