[英]Index was outside the bounds of the array. works in 1st example but doesn't in second
I have this example: ` 我有这个例子:
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"];
in my txt file I have: "Plate = -02-"
So, when I want to output whats in there, I do this: Variables.platefromtxt = dic["Plate"];
在我的txt文件中,我有:
"Plate = -02-"
因此,当我想输出其中的内容时,我这样做是: Variables.platefromtxt = dic["Plate"];
Which is working fine! 哪个工作正常!
I have tried to do another one: 我试图做另外一个:
var dicChatlog = File.ReadAllLines("chatlog.txt")
.Select(l => l.Split(new[] { '|' }))
.ToDictionary(s => s[0].Trim().ToString(), s => s[1].Trim().ToString());
This is what's inside my chatlog.txt: "^3-02 ^7Adorable [COP]^8 | ^7Whale Waileer"
这是我的chatlog.txt中的内容:
"^3-02 ^7Adorable [COP]^8 | ^7Whale Waileer"
So when I want to get "^7Whale Waileer"
I should do: Variables.name = dicChatlog["^3-02 ^7Adorable [COP]^8"]
因此,当我想获取
"^7Whale Waileer"
我应该这样做: Variables.name = dicChatlog["^3-02 ^7Adorable [COP]^8"]
When I'm trying to run the application, I get "Index was outside the bounds of the array." 当我尝试运行应用程序时,出现“索引超出数组范围”。 The first example works just fine, the second one however has that error.
第一个示例工作正常,但是第二个示例存在该错误。 Could any one tell me what I could be doing wrong?
谁能告诉我我可能做错了什么?
Thanks in advance 提前致谢
This should do the trick: 这应该可以解决问题:
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.