0

私はそのような構造を持っています:

FileStream fs = new FileStream(fileName, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            while (!sr.EndOfStream)
            {
                string line=sr.ReadLine();
                fullTextLines.Add(line);
            }

そしていくつかのテキスト:

string txt = "begin

middle

i am a string          

i am a string end"

「私は」で構成される最後の行のインデックスを取得したいです。例: 3 を取得する必要がありますが、使用するConsole.WriteLine(fullTextLines.LastIndexOf("GRID"));と -1 になりました。

4

3 に答える 3

6
fullTextLines.FindLastIndex(x => x.Contains("i am"));
于 2013-03-12T09:33:04.090 に答える
4
var result = File.ReadLines(fileName)
                 .Select((s,i) => new { Line = s, Index = i })
                 .LastOrDefault(x => x.Line.Contains("i am"));

int index = result == null ? -1 : result.Index;
于 2013-03-12T09:29:02.217 に答える