我一直在尝试制作某种消息解析器,它只获取我发送的消息。例如,如果我有这样的消息:
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!
我只需要获取 Viktor Bale
撰写的消息
这是我尝试过的代码:
for (int i = 0; i < wordsList.Count; i++)
{
if (wordsList[i].StartsWith(defaultName))
{
while (!wordsList[i].StartsWith(dialName))
{
messages.Add(wordsList[i]);
}
}
}
wordsList
是我的消息列表,从 txt 文件接收并由 ReadAllLines
读取
所以上面的消息只是列表。
defaultName
是我的名字,dialName
是我对话者的名字。
但是当我启动它时,我的应用程序就卡住了。我该怎么做?
最佳答案
您忘记增加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++]);
}
}
}
编辑:添加了安全边界检查。
关于c# - 消息解析器 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39147936/