Viktor Viktor - 3 months ago 18
C# Question

Message Parser C#

I've been trying to make some kind of message parser, that gets only my sent messages. For example if i had message like that:

Viktor Bale (11 aug. 2016 13:20:56):
Hi! How are you?

Not Viktor Bale (11 aug. 2016 13:20:56):
Hi! Good! And you?

Viktor Bale (11 aug. 2016 13:20:56):
Me too! And this message has
Two lines!

Not Viktor Bale (11 aug. 2016 13:20:56):
And this doesn't matter!


I need to get only messages written by
Viktor Bale

here is code, that i tryed:

for (int i = 0; i < wordsList.Count; i++)
{
if (wordsList[i].StartsWith(defaultName))
{
while (!wordsList[i].StartsWith(dialName))
{
messages.Add(wordsList[i]);
}
}
}


wordsList
is list of my messages, recieved from txt file and read by
ReadAllLines

So messages above is just list.

defaultName
is my name, and
dialName
is name of my interlocutor.

But when i launch it, my app simply freezes. How should i do that?

Answer

You are forgetting to increment i:

for (int i = 0; i < wordsList.Count; i++) 
{ 
    if (wordsList[i].StartsWith(defaultName)) 
    {
        while (i < worldList.Count && !wordsList[i].StartsWith(dialName)) 
        { 
            messages.Add(wordsList[i++]); 
        } 
    }    
} 

Edit: Added a safety bounds check.