当前位置: 首页 > 知识库问答 >
问题:

C#脚本问题-字符串不能正确地分隔成列表?

汤乐家
2023-03-14

我正在尝试将纯文本文件加载到列表(列表(字符串))中,由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之后的所有内容完全消失。

感谢任何帮助!

共有1个答案

姚乐家
2023-03-14

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

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语句。更容易阅读,我想适合你的需要。
  • 要小心将列表 添加到列表中。修改
    以后添加的列表时,就是修改添加的列表。换句话说,它们是通过引用添加的。
 类似资料:
  • 我有一个外国金融机构签名,我需要执行: FFI调用者需要提供一个缓冲区和该缓冲区的大小。Rust将向该缓冲区中填充一个字符串,最大值为,如果成功,则返回。FFI调用方应将字符串解释为ASCII。 鉴于我有一个

  • 问题内容: 我有一个电话号码(字符串),例如“ + 123-456-7890”,我想将其变成一个列表,如下所示:[+,1,2,3,-,....,0]。 为什么?因此,我可以遍历列表并删除所有符号,因此只剩下一个数字列表,然后可以将其转换回字符串。 解决此问题的最佳方法是什么?我遇到的所有解决方案均不适用,因为在数字之间没有任何特殊字符(因此我无法在其中分割字符串。) 有任何想法吗?我真的很感激!

  • 问题内容: 我需要在SQL Server 2012中将一列中的字符串拆分为一个字符,并将每个字符串拆分成它自己的列。 例如:如果我有一个栏,我需要把它拆分成,,,,,与每个这些转化为自己列。 要拆分的列的长度可能会有所不同,因此我需要使其尽可能地动态。 问题答案: 您可以这样做: 输出: 这是动态版本:

  • 我想像下面这样拆分这个字符串: {“我喜欢一个”、“橘子”、“以及”、“苹果”、“但主要是”、“苹果”、“苹果”,“苹果”,“橘子”,“是最好的水果。”} 在上面,我已经拆分了基于水果橙和苹果的字符串,但这两个水果也是子字符串列表的一部分。

  • 如何将过滤器列表拆分为单个过滤器元件?split2String在线程“main”java.util.regex中导致:异常。PatternSyntaxException:索引10或(|和)附近的未闭合组(

  • 我试图上传csv到我的mysql数据库使用jooq,但我得到以下错误。我在网上尝试了各种建议的解决方案,但都没能解决 我如何将csv上传到jooq 我确保文件在utf-8中,但是当有UTF-8字符记录时,无法保存在DB中并抛出上述错误。我确保使用 前端ajax: 我正在通过java rest从前端读取文件 并在传递给jooq之前在本地系统中递归写入文件 我将DB设置为接受utf-8,并进行了验证