我已经在图片中构建了一个数据表..我将在一个文本框中进行句子,如:"男孩踢球"我需要检查该句子中的每个单词,如果它存在于该表中,那么它将显示列表框中包含该单词的列的名称,例如:(=> Article)。 我做了所有的步骤,但我在检查和显示包含单词的列的名称的最后一步停止.. 我只是一个初学者,所以我很难编程完成最后一步,任何人都可以帮助我吗? datatable has three columns "Article","Noun","verb" and rows values:"the","boy","kick" and so on in other rows
DataTable dt = new DataTable();
DataColumn article = new DataColumn("Article", typeof(string));
dt.Columns.Add(article);
DataColumn noun = new DataColumn("Noun", typeof(string));
dt.Columns.Add(noun);
DataColumn verb = new DataColumn("Verb", typeof(string));
dt.Columns.Add(verb);
DataRow dr = dt.NewRow();
dr["Article"] = "the";
dr["Noun"] = "boy";
dr["Verb"] = "kick";
dt.Rows.Add(dr);
dr = dt.NewRow();
DataRow dd = dt.NewRow();
dd["Article"] = "a";
dd["Noun"] = "ball";
dd["Verb"] = "eat";
dt.Rows.Add(dd);
dd = dt.NewRow();
dataGridView1.DataSource = dt;
string s = textBox1.Text;
string[] st = s.Split(' ');
现在如何检查st []中的每个单词是否存在于表中 他将在
中显示他找到的单词和列名答案 0 :(得分:0)
希望有所帮助
using System;
using System.Data;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
DataColumn article = new DataColumn("Article", typeof(string));
dt.Columns.Add(article);
DataColumn noun = new DataColumn("Noun", typeof(string));
dt.Columns.Add(noun);
DataColumn verb = new DataColumn("Verb", typeof(string));
dt.Columns.Add(verb);
DataRow dr = dt.NewRow();
dr["Article"] = "the";
dr["Noun"] = "boy";
dr["Verb"] = "kick";
dt.Rows.Add(dr);
dr = dt.NewRow();
DataRow dd = dt.NewRow();
dd["Article"] = "a";
dd["Noun"] = "ball";
dd["Verb"] = "eat";
dt.Rows.Add(dd);
dd = dt.NewRow();
string s = "the boy eat the ball"; //textBox1.Text;
string[] st = s.Split(' ');
foreach (var str in st)
{
foreach (DataRow rows in dt.Rows)
{
if(str == rows["Article"].ToString())
Console.WriteLine("Word=" + str + " Column=Article");
else if (str == rows["Noun"].ToString())
Console.WriteLine("Word=" + str + " Column=Noun");
else if (str == rows["Verb"].ToString())
Console.WriteLine("Word=" + str + " Column=Verb");
}
}
}
}
}
输出:
Word = the Column = Article
Word = ball Column = Noun
Word = eat Column = Verb
Word = a Column = Article
Word = boy Column = Noun