![](/img/trans.png)
[英]How can I read from streamreader the first character from the first line? ( Example the Letter H that is in the txt file
[英]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.