简体   繁体   English

检查表中是否存在单词

[英]check if word exist in table

i have built a datatable as in picture .. i shall inter sentence in a textbox such as: "the boy kick the ball " i need to check each word of that sentence if it does exist in that table so it will display the name of the column that contains the word in a listbox such as:(the => Article).我已经建立了一个如图所示的数据表..我将在一个文本框中插入句子,例如:“男孩踢球”我需要检查该句子的每个单词,如果它确实存在于该表中,那么它将显示名称包含列表框中单词的列,例如:(=> 文章)。 i did all the steps but i stops in the last step of checking and displaying the name of the column that contains the word.. i'm just a beginner so it becomes hard to me to programming the last steps would any one help me please???我做了所有的步骤,但我在检查和显示包含这个词的列的名称的最后一步停止了。 ??? datatable has three columns "Article","Noun","verb" and rows values:"the","boy","kick" and so on in other rows数据表有三列“文章”,“名词”,“动词”和行值:“the”,“boy”,“kick”等在其他行

 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(' ');

now how to check each word in st[] if it does exist in the table he will display the word and the name of the column he found it in现在如何检查 st[] 中的每个单词,如果它确实存在于表中,他将显示单词和他在其中找到的列的名称

Hope it helps希望能帮助到你

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");
                }
            }
        }
    }
}

Output:输出:

Word=the Column=Article字=列=文章

Word=ball Column=Noun词=球列=名词

Word=eat Column=Verb字=吃列=动词

Word=a Column=Article字=一列=文章

Word=boy Column=Noun字=男孩列=名词

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM