所以,我有一个文本文件,它看起来像这样:查找包含特定字符串的行和随机选择一个在C#
[hello]Hi
[hello]welcome back
[hello]Hello sir
[goodbye]goodbye sir
[goodbye]until next time
...
这里是代码的一部分:
string[] responseLines = File.ReadAllLines(@"responses.txt");
Random rand = new Random();
string response;
case "hello":
chatBox.Items.Add(Me);
response = responseLines[rand.Next(responseLines.Length)];
JARVIS.SpeakAsync(response);
chatBox.Items.Add("Jarvis: " + response);
break;
问题是我只想找到包含[hello]
的行,然后随机选择其中的一行。
也可以!正是我正在寻找:) – techProdigy
@techProdigy你可以请upvote答案? tks –
我试过了,但我不能? Im新到这个网站。我认为我需要至少15个声望? – techProdigy