首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#从日志文件中读取特定属性

C#从日志文件中读取特定属性
EN

Stack Overflow用户
提问于 2017-12-03 19:42:25
回答 1查看 50关注 0票数 0

在我的M.U.G.E.N锦标赛程序中,我想处理日志文件中的匹配结果,这个日志文件是由游戏创建的。日志如下所示:

代码语言:javascript
复制
[Match 1]
totalmatches = 1
team1.1 = 
team2.1 = 
stage = stages/kowloon.def

[Match 1 Round 1]
winningteam = 1
timeleft = -1.00
p1.name = Knuckles the Echidna
p1.life = 269
p1.power = 0
p2.name = Shadow the Hedgehog
p2.life = 0
p2.power = 2684

[Match 1 Round 2]
winningteam = 2
timeleft = -1.00
p1.name = Knuckles the Echidna
p1.life = 0
p1.power = 1510
p2.name = Shadow the Hedgehog
p2.life = 586
p2.power = 2967

[Match 1 Round 3]
winningteam = 2
timeleft = -1.00
p1.name = Knuckles the Echidna
p1.life = 0
p1.power = 3000
p2.name = Shadow the Hedgehog
p2.life = 789
p2.power = 777

我想要的是处理最后一个winningteam属性来确定匹配的结果。要做到这一点,最有效的方法是什么?(也许是LINQ)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-03 19:53:21

您可以使用File.ReadLines作为IEnumerable<string>返回行的枚举。

代码语言:javascript
复制
// string path contains file path and file name.
string line = File.ReadLines(path)
    .LastOrDefault(ln => ln.StartsWith("winningteam ="));

现在在=中拆分字符串,修剪第二部分,然后将团队号作为字符串

代码语言:javascript
复制
if (line != null) {
    string team = line.Split('=')[1].Trim();
    // With "winningteam = 2", [0] = "winningteam ", [1] = " 2"

    // Optionally convert it to a number
    int teamNo = Int32.Parse(team);

    ...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47622492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档