c# - 消息解析器 C#

标签 c# list

我一直在尝试制作某种消息解析器,它只获取我发送的消息。例如,如果我有这样的消息:

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/

相关文章:

c# - JavaScriptSerializer 反序列化为嵌套对象

c# - 关闭 MySQL 连接在 .NET 应用程序中重要吗? (准确地说,C#)

java - 如何排序 List<List<String>>

python打印正则表达式匹配产生空列表

python - 字典列表,按列表键无交集

python - 迭代同一个列表两次并避免在 python 中重复

c# - 如何在 C# 中使用 wkHtmltoPdf 从静态 html 文件生成 PDf 文件

c# - ASP.NET网站跨域登录

c# - EF Power Tools 被自引用表弄糊涂了

python - 在python中按扩展名对文件路径进行排序