Reputation: 265
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
Upvotes: 0
Views: 181
Reputation: 126
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. For example: If we have a string "ANIMAL" and "ANIMATE" both the strings share "ANIMA". This kind of data can be stored in a trie ( sharing reduces memory ).
Search Algorithm:- For every word from database look for the word in trie in Order of ( length of the word ).
Upvotes: 0
Reputation:
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.
Upvotes: 0
Reputation: 12786
You could also make use of the Regex class like this:
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);
}
}
Upvotes: 0
Reputation: 6834
Populate the strings from the text file into a collection (like a List or a 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;
Upvotes: 2