数组中txt文件中的特定文本

本文关键字:文本 文件 txt 数组 | 更新日期: 2024-09-19 16:58:20

我正在为我的最后一个c#项目制作一个"谁想成为百万富翁"游戏,我有15个txt,每个有3个问题/答案。示例:
Whats the capital of Brasil? || Rio de Janeiro || Brasilia || Sao Paulo || Curitibia || Brasilia ||

我想把这个问题存储到一个数组[0,0]中,答案在接下来的位置[0,1到0,5],这就是为什么有||要分开的原因
如何为每个txt文件上的3个问题做到这一点
感谢

数组中txt文件中的特定文本

假设您有一个文件,其中包含您给定格式的问题和答案,下面是一个可以增强的示例:

       string[] wordsArray = null;
       string s = string.Empty;
       string path = "file_1.txt";
       string[] lines = System.IO.File.ReadAllLines(path);
       foreach (string aLine in lines)
       {
            s = aLine.Replace("||", "|");
            wordsArray = s.Split('|');
            //now you have the question in wordsArray[0], and the answers in the following
            //array cells ([1],[2], etc.)
            //You can do what you want here, including building a 2-d array using wordsArray.
       }

我不确定你为什么用||而不是|或者,我也不确定为什么最后一个答案是定界的(这将在上面的解决方案中创建一个空单元格),你可能会使用类似的东西:

巴西的首都是什么?|里约热内卢|Brasilia|Sao Paulo|Curitibia|Brasilia