简体   繁体   English

在字符串中查找数组中的字符串

[英]finding in a string a string from an array

I' don't know exactly how to start with the following. 我不确切知道如何从以下开始。 On one side, I have a text file containing a hundred names On the other side, I've got a column in a datatable containing strings. 一方面,我有一个包含一百个名字的文本文件另一方面,我在包含字符串的数据表中有一个列。 I try to find in each of these strings one of the names contained in the text file. 我尝试在每个字符串中找到文本文件中包含的名称之一。 I guess I'll have to fill an array with the content of my textfile, and then iterate through the array while searching in the string ? 我想我必须用我的文本文件的内容填充一个数组,然后在搜索字符串时迭代数组?

Any idea on how to start this one would be appreciated. 任何关于如何开始这个的想法将不胜感激。 Cheers 干杯

Populate the strings from the text file into a collection (like a List or a Dictionary. 将文本文件中的字符串填充到集合中(如List或Dictionary)。

Then iterate thru the strings from the datatable column and simply check if the string is in the collection: 然后通过数据表列中的字符串进行迭代,只需检查字符串是否在集合中:

if (nameCollection.Contains(namestring)) return true;

You could also make use of the Regex class like this: 你也可以像这样使用Regex类:

        string text = "a b c d e f";
        List<String> dbStrings = new List<string>();
        dbStrings.AddRange(new string[] { "a", "b", "c" });
        foreach (string dbString in dbStrings) {
            string pattern = @"(?<=^|\s)" + dbString + @"(?=\s|$)";
            if (Regex.IsMatch(text, pattern)) {
                Console.WriteLine(dbString);
            }
        }

You can first add all name into list and then, add all rows of particular column in another list and then check whether string contain name or not....I have write down code for your scenario: 您可以先将所有名称添加到列表中,然后在另一个列表中添加特定列的所有行,然后检查字符串是否包含名称....我已为您的方案写下代码:

        List<string> listName = new List<string>();
        using (StreamReader reader = new StreamReader("C:\\file1.txt"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                listName.Add(line); // Adding name in list
            }
        }
        List<string> listRowsStr = new List<string>();
        List<string> listRowContainName = new List<string>();

        //Adding all rows of particular column in list
        listRowsStr=(from name in dt.AsEnumerable()
                   select name.Field<string>("column_name")).ToList<string>();
        foreach (string name in listName)
        {
            foreach (string rowStr in listRowsStr)
            {
                if (rowStr.Contains(name))
                {
                    listRowContainName.Add(rowStr);//Adding the name containing string into the sepearte list i.e.listRowsStr
                }
            }
        }

listRowContainName contains all string which have name of text file. listRowContainName包含具有文本文件名称的所有字符串。

Store the words in the file into a data structure such that looking up is easy for the strings. 将文件中的单词存储到数据结构中,以便查找字符串很容易。 We ca use a Trie as it uses a lot of shared structure. 我们使用Trie,因为它使用了很多共享结构。 For example: If we have a string "ANIMAL" and "ANIMATE" both the strings share "ANIMA". 例如:如果我们有一个字符串“ANIMAL”和“ANIMATE”,则两个字符串共享“ANIMA”。 This kind of data can be stored in a trie ( sharing reduces memory ). 这种数据可以存储在trie中(共享减少内存)。

Search Algorithm:- For every word from database look for the word in trie in Order of ( length of the word ). 搜索算法: - 对于来自数据库的每个单词,按顺序查找单词中的单词(单词的长度)。

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

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