我正在尝试将纯文本文件加载到列表(列表(字符串))
中,由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));
}
代码中有一个问题,我似乎找不到如何修复它。现在我得到的结果是:textbox1
输出text1,textbox2
输出text1+text2,textbox3
输出text1+text2+text3,以此类推。
在将ConvertedList
列表添加到DialogBlock
列表之后,我再次尝试将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();
即使我误解了你的确切目标,我认为你需要注意的是:
foreach
循环。更容易阅读。switch
语句。更容易阅读,我想适合你的需要。列表
添加到列表
中。修改