繁体   English   中英

我怎么能有一个 .txt 文件,我从中读取行并将它们放入一个数组中,但每一行都在某个字符后被拆分

[英]How can I have a .txt file that I read lines from and put them into an array, but every line is split after a certain character

我正在制作一个控制台应用程序来导航我的电脑。

我有一个叫做 Askforcmd() 的函数,它可以让你写一个命令。 它测试您是否使用 ifs 和 else ifs 编写了特定内容(您编写的内容存储在字符串“commands”中)。

我正在尝试将所有游戏写入 .txt,以换行符分隔,并在(以“^”分隔)之后写入位置,(例如:

传送门 2^C:/PathOfGame

传送门^C:/路径

) 并让代码知道如果你写了一个游戏的名字,它应该在后面的路径打开文件(我知道如何打开文件)。

我知道如何从 txt 中读取并将其放入数组中,但是如何让它停止读取某个字符后的行并将其存储在不同的数组中? 到目前为止我所拥有的:

else if (lines.Any(commands.Contains))
        {
           /*Code to check what game to open and at 
             what path
           */
          Askforcmd();
        }

else if (commands == "games")
        {
            Console.Write("\n");
            int count = lines.Length;
            int numsss = 0;
            int ds;
            while (numsss != count)
            {
                ds = numsss + 1;
                Console.WriteLine(ds + ": " + lines[numsss]);

                numsss++;
            }
            Askforcmd();
        }

当我运行代码并编写“游戏”时,它会在游戏之前列出一个数字。

1:传送门2

2:传送门

等等

您可以获取读取的第一个字符串并在其上运行 split 命令。

Array[] newArray = lines[numsss].split('^');

你会得到一个等于文件名的新数组,路径在第二个元素中。

编辑:根据您的评论,您有奇怪的要求。 你可以这样做:

//assume your previous array is lines
List<string> temp = new List<string>;
for each (string line in lines)
{

    temp.Add(line.split('^')[0]);
    temp.Add(line.split('^')[1]);
}
String[] outArray = temp.toArray();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM