【发布时间】:2020-07-12 20:46:22
【问题描述】:
我有一个 foreach 循环,对于每一行,我需要获取前 2 行并将它们存储在一个变量中。我该怎么做?我已经尝试了很长时间。
文件大小约5mb
我的 foreach 循环:
foreach (string line in File.ReadAllLines("DeadByDaylightCopy.log"))
{
if (line.Contains("LogCustomization: --> TW"))
{
killer = "Wraith";
}
else if (line.Contains("LogCustomization: --> TR"))
{
killer = "Trapper";
}
else if (line.Contains("LogCustomization: --> HK"))
{
killer = "Spirit";
}
else if (line.Contains("LogCustomization: --> MK"))
{
killer = "Plague";
}
else if (line.Contains("LogCustomization: --> FK"))
{
killer = "Pig";
}
else if (line.Contains("LogCustomization: --> OK"))
{
killer = "GhostFace";
}
else if (line.Contains("LogCustomization: --> TN"))
{
killer = "Nurse";
}
else if (line.Contains("LogCustomization: --> KK"))
{
killer = "Legion";
}
else if (line.Contains("LogCustomization: --> BE"))
{
killer = "Huntress";
}
else if (line.Contains("LogCustomization: --> TC"))
{
killer = "Billy";
}
else if (line.Contains("LogCustomization: --> WI"))
{
killer = "Hag";
}
else if (line.Contains("LogCustomization: --> GK"))
{
killer = "Clown";
}
else if (line.Contains("LogCustomization: --> SD"))
{
killer = "Freddy";
}
else if (line.Contains("LogCustomization: --> DO"))
{
killer = "Doctor";
}
else if (line.Contains("LogCustomization: --> CA"))
{
killer = "Cannibal";
}
else if (line.Contains("LogCustomization: --> MM"))
{
killer = "Myers";
}
else if (line.Contains("LogCustomization: --> UK"))
{
killer = "Deathslinger";
}
else if (line.Contains("LogCustomization: --> SwedenKiller"))
{
killer = "Oni";
}
else if (line.Contains("LogCustomization: --> QK"))
{
killer = "Demogorgon";
}
例如。文本文件
LogCustomization: --> TR_HEAD_03
LogCustomization: --> TR_TORSO_P01
LogCustomization: --> TR_LEGS_02
fsdfs
fsdfs
LogCustomization: --> KK_HEAD_04
LogCustomization: --> KK_BODY_P01
LogCustomization: --> KK_LEGS_P01
dfsdfs
LogCustomization: --> TW_HEAD02_LP01
LogCustomization: --> TW_BODY03_LP01
LogCustomization: --> TW_WEAPON07_P01
sfsdfs
LogCustomization: --> CM_HEAD_P01
LogCustomization: --> CM_TORSO_06
LogCustomization: --> CM_LEGS_LP01
sdfsf
sdfsdf
看起来您的帖子主要是代码;请添加更多细节。 看起来您的帖子主要是代码;请添加更多详细信息。
【问题讨论】:
-
将所有行存储在一个字符串数组中。遍历数组的循环以 i=0 开始。这样您就可以通过
arr[i-1]和arr[i-2]访问前两行 -
底部的文字(“//2short //2short”等)是什么意思?
-
你能举一个你试图阅读的文件的例子吗?循环中测试的关键字是否以 3 行间隔显示?
-
这个文件有多大? ReadAllLine 从文件中加载所有内容,这可能对您的内存负载和性能非常重要
-
@Steve 帖子已编辑