gpt4 book ai didi

c# - 如何从文本文件中的另一行向下读取 X 行?

转载 作者:行者123 更新时间:2023-11-30 13:48:15 25 4
gpt4 key购买 nike

我有一个文本文件,我将其加载到一个字符串数组中。该文件的内容如下所示:

OTI*IA*IX*NA~
REF*G1*J EVERETTE~
REF*11*0113722462~
AMT*GW*229.8~
NM1*QC*1*JENNINGS*PHILLIP~
OTI*IA*IX*NA~
REF*G1*J EVERETTE~
REF*11*0113722463~
AMT*GW*127.75~
NM1*QC*1*JENNINGS*PHILLIP~
OTI*IA*IX*NA~
REF*G1*J EVERETTE~
REF*11*0113722462~
AMT*GW*10.99~
NM1*QC*1*JENNINGS*PHILLIP~
...

我正在寻找以 OTI 开头的行,如果它后跟“IA”,那么我需要从以 REF*11 开头的行中获取 10 位数字。到目前为止,我有这个:

string[] readText = File.ReadAllLines("myfile.txt");

foreach (string s in readText) //string contains 1 line of text from above example
{
string[] currentline = s.Split('*');

if (currentline[0] == "OTI")
{
//move down 2 lines and grab the 10 digit
//number from the line that starts with REF*11
}
}

我需要的行总是在当前 OTI 行之后的 2 行。如何访问当前行下 2 行的行?

最佳答案

而不是使用 foreach()你可以使用 for(int index = 0; index < readText.Length; index++)然后你知道你正在访问的线路,你可以轻松地说 int otherIndex = index + 2

string[] readText = File.ReadAllLines("myfile.txt");

for(int index = 0; index < readText.Length; index++)
{
string[] currentline = readText[index].Split('*');

if (currentline[0] == "OTI")
{
//move down 2 lines and grab the 10 digit
//number from the line that starts with REF*11
int refIndex = index + 2;
string refLine = readText[refIndex];
}
}

关于c# - 如何从文本文件中的另一行向下读取 X 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13126702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com