现在我有这个代码检查字符串中是否有任何单词:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PlayerIOClient;
public static Connection conn;
public static Client client;
public static Dictionary<int, string> users = new Dictionary<int, string>();
public static Dictionary<string, string> corList = new Dictionary<string, string>();
public bool isConnected;
static void OnMessage(object sender, PlayerIOClient.Message m)
{
if (m.Type == "init")
{
conn.Send("access", wldcode);
conn.Send("init2");
corList.Add("hes", "he's"); //Example of one item on the list.
conn.Send("say", "Grammar fixer bot connected. Repare to be corrected.");
}
if (m.Type == "add")
{
users.Add(m.GetInt(0), m.GetString(1));
}
if (m.Type == "left")
{
users.Remove(m.GetInt(0));
}
if (m.Type == "say")
{
if (users.ContainsKey(m.GetInt(0)))
{
string username = users[m.GetInt(0)];
if (corList.Keys.Any(m.GetString(1).Contains)) //Problem is here.
{ //m.GetString(1) Are usually long strings, such as sentences.
conn.Send("say", "Grammar Fixer: " + corList[]);
}
}
}
}
但是,我无法获取在该字中检测到的特定密钥或此密钥的值。这本词典也将包含很多定义,所以只有一个特定单词的if然后才能起作用。
答案 0 :(得分:0)
很难说没有看到corList
是什么或m.GetString(1).Contains
是什么。所以我必须做很多关于你想做什么的猜测,但你可以试试这个:
var key = m.GetString(1);
if (corList.Keys.Any(key))
{
conn.Send("say", string.format("Grammar Fixer: {0}, not {1}!", corList[key], key));
}
答案 1 :(得分:0)
var key = m.GetString(1);
string correction = null;
if (corList.TryGetValue(key, out correction))
{
conn.Send("say", string.format("Grammar Fixer: {0}, not {1}!", correction, key));
}