Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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#脚本问题-字符串未正确分隔到列表中?_C#_List - Fatal编程技术网

C#脚本问题-字符串未正确分隔到列表中?

C#脚本问题-字符串未正确分隔到列表中?,c#,list,C#,List,我试图将纯文本文件加载到列表(列表(字符串)),由EndTextBlock(进入列表)或EndDialogBlock(进入列表(列表))分隔。每个对话框块可以有不同的长度 我有一个简单的读取文件行,它读取txt文件的每一行,并将每一行作为新项添加到TXTDialogFileList中。下面的代码就是我试图整理原始数据并制作双重列表的地方 //loop to increment the list's counter for (int i = 0; i < TXTDialogFileList.

我试图将纯文本文件加载到
列表(列表(字符串))
,由
EndTextBlock
(进入列表)或
EndDialogBlock
(进入
列表(列表)
)分隔。每个对话框块可以有不同的长度

我有一个简单的读取文件行,它读取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));
}    
//循环以递增列表的计数器
for(int i=0;i
代码中有一个问题,我似乎无法找到如何解决它。现在我得到的结果是:
Textbox1
输出text1,
Textbox2
输出text1+text2,
Textbox3
输出text1+text2+text3,依此类推

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


感谢您的帮助

看看我是否理解你的目标。以下是我的示例文本:

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();
List TXTDialogFileList=newlist();
StreamReader sr=新的StreamReader(@“sampletext”);
而(sr.Peek()>=0)
{
添加(sr.ReadLine());
}
高级关闭();
var currentList=新列表();
var resultList=新列表();
var currentLines=“”;
foreach(TXTDialogFileList中的字符串行)
{
道岔(线路)
{
案例“EndTextBlock”:
currentList.Add(currentLines);
currentLines=“”;
打破
案例“EndDialogBlock”:
如果(currentLines.Length>0)currentList.Add(currentLines);//在EndDialogBlock之前假设隐式EndTextBlock
Add(new List(currentList));//通过引用添加的列表,因此在下一行清除它之前,请确保它是一个新列表
currentList.Clear();
currentLines=“”;
打破
违约:
currentLines+=行+“\n”;
打破
}
}
//输出列表以显示结果是否正确
foreach(结果列表中的var textBlock)
{
Console.WriteLine(“启动对话框块”);
foreach(文本块中的变量文本)
{
Console.WriteLine(“启动文本块”);
控制台。写入(文本);
}
}
Console.Read();
即使我误解了你的确切目标,我认为你需要注意的是:

  • 使用
    foreach
    循环。更容易阅读
  • 使用
    开关
    语句。阅读起来容易得多,我想这是合适的 满足你的需要
  • 小心将
    列表添加到
    列表中。当您修改时
    您稍后添加的列表将修改添加的列表。换句话说 它们是通过引用添加的单词

看起来这完全符合需要,谢谢!我不知道那个参考位——我想它只是把数据复制过来,就这样结束了。这一定是我的版本的问题,但你的版本确实是浓缩的!谢谢。