24

文字列が比較される場合、txtファイルで文字列を見つけたいのですが、パラメーターとして使用している別の文字列まで行を読み続ける必要があります。

例:

CustomerEN //search for this string
...
some text which has details about the customer
id "123456"
username "rootuser"
...
CustomerCh //get text till this string

それ以外の場合は、詳細が必要です。

次のようにlinqを使用して「CustomerEN」を検索しています。

File.ReadLines(pathToTextFile).Any(line => line.Contains("CustomerEN"))

しかし、今は「CustomerCh」まで行 (データ) を読み取って詳細を抽出することにこだわっています。

4

5 に答える 5

13

インデックスについて知らないのでwhile使用する必要があります。foreach以下はサンプルコードです。

int counter = 0;
string line;

Console.Write("Input your search text: ");
var text = Console.ReadLine();

System.IO.StreamReader file =
    new System.IO.StreamReader("SampleInput1.txt");

while ((line = file.ReadLine()) != null)
{
    if (line.Contains(text))
    {
        break;
    }

    counter++;
}

Console.WriteLine("Line number: {0}", counter);

file.Close();

Console.ReadLine();
于 2012-10-12T10:05:56.510 に答える
0

ローリングがここに投稿した方法を少し試して、同じファイル内の複数の行を最後まで見つけました。これは私のために働いたものです:

                foreach (var line in File.ReadLines(pathToFile))
                {
                    if (line.Contains("CustomerEN") && current == null)
                    {
                        current = new List<string>();
                        current.Add(line);
                    }
                    else if (line.Contains("CustomerEN") && current != null)
                    {
                        current.Add(line);
                    }
                }
                string s = String.Join(",", current);
                MessageBox.Show(s);
于 2017-06-09T19:01:28.733 に答える