-2

テキスト行を含むテキスト ファイルがあります。たとえば、次の行が条件を満たしている場合に、特定の行をリスト ボックスに追加しようとしています。

行が # で始まる場合、次の行が @ で始まり、その後のすべての行が @ で始まる場合は、その行を追加します。

#add this line
@add this line
@add this line
@add this line
#dont add because the next line is not a @
#dont add because the next line is not a @
#dont add because the next line is not a @
#dont add because the next line is not a @
#add this line
@add this line
@add this line
#dont add because the next line is not a @
#add this line
@add this line  
#add this line
@add this line

これがシーンを作ることを願っています

4

1 に答える 1

3

String の StartsWith メソッドを使用http://msdn.microsoft.com/en-us/library/system.string.startswith.aspx

あなたの機能全体が次のようになります

var lines = File.ReadAllLines(yourpath);
var resultLines = new List<string>();
bool adding = false;
for(int i=0;i<lines.Length;i++)
{
    var line = lines[i];
    if((line.StartsWith("#") && i < lines.Length-1 && lines[i+1].StartsWith("@"))
       || adding && line.StartsWith("@"))
        adding = true;
    else if(i < lines.Length-1 && !lines[i+1].StartsWith("@"))
        adding = false;
    if(adding)
        resultLines.Add(line);
}
于 2012-08-26T20:08:39.683 に答える