C#脚本问题 - 字符串没有正确分成列表?

时间:2015-03-07 23:48:47

标签: c# list

我正在尝试将纯文本文件加载到List(List(string)),由EndTextBlock(在列表中)或EndDialogBlock(在List(List)中)分隔。每个Dialog块的长度可以不同。

我有一个简单的读取文件行,它读取我的txt文件的每一行,并将每一行添加为TXTDialogFileList中的新项。下面的代码是我试图理清原始数据并制作双重列表的地方。

//loop to increment the list's counter
for (int i = 0; i < TXTDialogFileList.Count; i++)   
{
    DialogBlock = new List<string> ();

    bool NextRun = false;
    do
    {   //do-while loop to merge multiple lines into 1 slot in the list
        //DialogBlock = new List<string> ();

        if ( (i + 1) < TXTDialogFileList.Count)
        {
            if (   TXTDialogFileList[i+1] == "EndDialogBlock" 
                || TXTDialogFileList[i+1] == "EndTextBlock"     )
            {
                //line break removal to next in list
                //TXTDialogFileList[i+1].Remove(TXTFileList[i+1]);
                //print (TXTDialogFileList[i]);
                //print (RunCounter + " " + (i + 1));

                DialogBlock.Add (TXTDialogFileList[i]);

                //DialogConvertedList[RunCounter].Add (TXTDialogFileList[i]);
                if (TXTDialogFileList[i+1] == "EndDialogBlock")
                {
                    //only increment counter for converted list if it's end dialog
                    RunCounter ++;  
                    DialogConvertedList.Add (DialogBlock);
                    NextRun = false;
                }
                else if (TXTDialogFileList[i+1] == "EndTextBlock")
                {
                    DialogConvertedList.Add (DialogBlock);
                    NextRun = true;
                    //DialogBlock = new List<string>();
                }
                TXTDialogFileList.Remove(TXTDialogFileList[i+1]);
            }

            else
            {
                //merging next line to current line in list (line break in between)
                TXTDialogFileList[i] = TXTDialogFileList[i] 
                                        + '\n' + TXTDialogFileList[i+1];
                TXTDialogFileList.Remove(TXTDialogFileList[i+1]);
                NextRun = true;
            }
        }
        else
        {
            //no more lines in file
            NextRun = false;
        }
    } while ((NextRun == true));
}    

代码中存在问题,我似乎无法找到解决方法。现在我最终得到的结果是:Textbox1输出text1,Textbox2输出text1 + text2,Textbox3输出text1 + text2 + text3,依此类推。

我在将列表添加到DialogBlock列表后尝试再次将ConvertedList声明为新列表,但这最终使text1之后的所有内容完全消失。

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

让我们看看我是否了解你的目标。这是我的示例文本:

The first line of text
The second line of text
EndTextBlock
The third line of text
The fourth line of text
EndTextBlock
The fifth line of text
The sixth line of text
EndDialogBlock
The seventh line of text
The eight line of text
EndTextBlock
EndDialogBlock
The ninth line of text
The tenth line of text
EndTextBlock
The eleventh line of text
The twelfth line of text
EndDialogBlock

下面是一些处理它的代码。我保留了您的变量名称TXTDialogFileList,但我认为它可能是一个误导性的名称。我更改了其他许多内容,因为它们引用了代码段之外的元素:

    List<String> TXTDialogFileList = new List<String>();

    StreamReader sr = new StreamReader(@"sampletext");
    while (sr.Peek() >= 0)
    {
        TXTDialogFileList.Add(sr.ReadLine());
    }
    sr.Close(); 

    var currentList = new List<String>();   
    var resultList = new List<List<String>>();  
    var currentLines = "";

    foreach (String line in TXTDialogFileList)
    {
        switch (line)
        {
            case "EndTextBlock":
                currentList.Add(currentLines);
                currentLines="";
                break;
            case "EndDialogBlock":
                if (currentLines.Length > 0) currentList.Add(currentLines); // assuming implicit EndTextBlock before EndDialogBlock
                resultList.Add(new List<String>(currentList)); // list<string> added by reference so make sure it is a new list before it is cleared on next line
                currentList.Clear(); 
                currentLines="";
                break;
            default:
                currentLines += line + "\n";
                break;  
        }
    }

    // Output the List<List<String>> to show results are correct
    foreach (var textBlock in resultList)
    {
        Console.WriteLine("Start DialogBlock");
        foreach(var text in textBlock)
        {
            Console.WriteLine("Start TextBlock");
            Console.Write(text);
        }
    }

    Console.Read();

即使我误解了你的确切目标,我认为你需要注意的事项是:

  • 使用foreach循环。更容易阅读。
  • 使用switch声明。更容易阅读,我怀疑合适 满足您的需求。
  • 小心向List<String>添加List。当你修改
    时 您稍后添加的列表,您正在修改添加的列表。其他 他们是由参考文献添加的。