[英]Index was outside the bounds of the array. works in 1st example but doesn't in second
我有這個例子:
var dic = File.ReadAllLines("settings.txt")
.Select(l => l.Split(new[] { '=' }))
.ToDictionary(s => s[0].Trim().ToString(), s => s[1].Trim().ToString());
Variables.platefromtxt = dic["Plate"];
在我的txt文件中,我有: "Plate = -02-"
因此,當我想輸出其中的內容時,我這樣做是: Variables.platefromtxt = dic["Plate"];
哪個工作正常!
我試圖做另外一個:
var dicChatlog = File.ReadAllLines("chatlog.txt")
.Select(l => l.Split(new[] { '|' }))
.ToDictionary(s => s[0].Trim().ToString(), s => s[1].Trim().ToString());
這是我的chatlog.txt中的內容: "^3-02 ^7Adorable [COP]^8 | ^7Whale Waileer"
因此,當我想獲取"^7Whale Waileer"
我應該這樣做: Variables.name = dicChatlog["^3-02 ^7Adorable [COP]^8"]
當我嘗試運行應用程序時,出現“索引超出數組范圍”。 第一個示例工作正常,但是第二個示例存在該錯誤。 誰能告訴我我可能做錯了什么?
提前致謝
這應該可以解決問題:
var dicChatlog = File.ReadAllLines("chatlog.txt")
.Where(l => l.Contains("|"))
.Select(l => l.Split(new[] { '|' }))
.ToDictionary(s => s[0].Trim().ToString(), s => s[1].Trim().ToString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.