Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从文本文件中的另一行向下读取X行?_C#_Arrays_Text Files - Fatal编程技术网

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

C# 如何从文本文件中的另一行向下读取X行?,c#,arrays,text-files,C#,Arrays,Text Files,我有一个文本文件,可以加载到字符串数组中。文件的内容如下所示: OTI*IA*IX*NA~参考文献G1*J EVERETTE~参考文献11*0113722462~ AMT*GW*229.8~NM1*QC*1*JENNINGS*PHILLIP~OTI*IA*IX*NA~REF*G1*J 埃弗雷特~参考号*11*0113722463~金额*GW*127.75~ NM1*QC*1*JENNINGS*PHILLIP~OTI*IA*IX*NA~REF*G1*J EVERETTE~ 参考文献*11*0113

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

OTI*IA*IX*NA~
参考文献G1*J EVERETTE~
参考文献11*0113722462~
AMT*GW*229.8~
NM1*QC*1*JENNINGS*PHILLIP~
OTI*IA*IX*NA~
REF*G1*J 埃弗雷特~
参考号*11*0113722463~
金额*GW*127.75~
NM1*QC*1*JENNINGS*PHILLIP~
OTI*IA*IX*NA~
REF*G1*J EVERETTE~
参考文献*11*0113722462~
金额*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条线路。如何访问当前行下方两行的行?

您可以使用
for(int index=0;indexforeach()
然后你知道你正在访问的行,你可以很容易地说
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];
    }
}
string[]readText=File.ReadAllLines(“myfile.txt”);
for(int index=0;index
为什么不使用System.Text.RegularExpressions中定义的Regex.Match或Regex.matches来使用正则表达式匹配?您还可以查看字符串模式匹配算法,如Knuth-Morris-Pratt算法。

这看起来像一个EDI文件!啊,伊迪,回忆

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] == "REF"&&currentline[1] == "11")
    {
        found=false;
        needed=current+2;
    }
}
好消息是EDI文件是分隔的,就像大多数CSV文件格式一样。您可以使用任何标准CSV文件库将EDI文件加载到一个巨大的数组中,然后按位置对其进行迭代

我在这里发布了我的开源CSV库,如果有帮助,请随意使用。您只需指定“星号”作为分隔符:

此时,您可以像平常一样迭代数据表

for (int i = 0; i < dt.Rows.Count; i++) {
    if (dt.Rows[i][0] == "OTI") {
        Console.WriteLine("The row I want is: " + dt.Rows[i + 2][0]);
    }
}
for(int i=0;i
string[]readText=File.ReadAllLines(“myfile.txt”);
对于(int-linenum=0;linenum
关于:

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

for (int i = 0; i < readText.Length; i++)
{
    if (readText[i].StartsWith("OTI") && readText[i+2].StartsWith("REF*11")){
       string number = readText[i+2].Substring("REF*11".Length, 10);
       //do something 
    }
}
string[]readText=File.ReadAllLines(“myfile.txt”);
for(int i=0;i
谢谢各位。。这是我想到的,但我也在阅读你的答案,因为我知道我会学到一些东西!再次感谢

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

int i = 0;
foreach (string s in readText)
{
    string[] currentline = s.Split('*');

    if (currentline[0] == "OTI")
    {
        lbRecon.Items.Add(readText[i+2].Substring(8, 9));
    }
i++;
}

如果您想使用正则表达式来标记这些项并创建动态实体,下面是这样一种模式

string data = @"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~";

string pattern = @"^(?<Command>\w{3})((?:\*)(?<Value>[^~*]+))+";

var lines = Regex.Matches(data, pattern, RegexOptions.Multiline)
                 .OfType<Match>()
                 .Select (mt => new
                 {
                    Op   = mt.Groups["Command"].Value,
                    Data = mt.Groups["Value"].Captures.OfType<Capture>().Select (c => c.Value)
                 }
                 );
string data=@“NM1*QC*1*JENNINGS*PHILLIP~
OTI*IA*IX*NA~
参考文献*G1*J埃弗雷特~
参考文件*11*0113722463~
金额*GW*127.75~
NM1*QC*1*JENNINGS*PHILLIP~
OTI*IA*IX*NA~
参考文献*G1*J埃弗雷特~
参考编号*11*0113722462~
金额*GW*10.99~
NM1*QC*1*JENNINGS*PHILLIP~”;
字符串模式=@“^(?\w{3})((?:\*)(?[^~*]+)+”;
var lines=Regex.Matches(数据、模式、RegexOptions.Multiline)
第()类
.选择(mt=>new
{
Op=mt.Groups[“命令”]值,
Data=mt.Groups[“Value”]。捕获.OfType()。选择(c=>c.Value)
}
);
这将生成一个项目列表,您可以将业务逻辑应用于:


是的。。好,伊迪。我会查一下你的图书馆。。非常感谢。我不知道For语句可以这样使用。。非常感谢。
string[] readText = File.ReadAllLines("myfile.txt");

int i = 0;
foreach (string s in readText)
{
    string[] currentline = s.Split('*');

    if (currentline[0] == "OTI")
    {
        lbRecon.Items.Add(readText[i+2].Substring(8, 9));
    }
i++;
}
string data = @"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~";

string pattern = @"^(?<Command>\w{3})((?:\*)(?<Value>[^~*]+))+";

var lines = Regex.Matches(data, pattern, RegexOptions.Multiline)
                 .OfType<Match>()
                 .Select (mt => new
                 {
                    Op   = mt.Groups["Command"].Value,
                    Data = mt.Groups["Value"].Captures.OfType<Capture>().Select (c => c.Value)
                 }
                 );