Reputation: 19
I already used this code, but it takes much more time to search bulk data. My text file contains more than a million words, but all words are in alphabetical order. How to search the words in a dictionary manner.
int aCounter = 0; string aWordInTextFile;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Rider\Documents\Visual Studio 2012\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Resources\enable3.txt");
while((aWordInTextFile = file.ReadLine()) != null)
{
Console.WriteLine (aWordInTextFile);
if(textBox1.Text == aWordInTextFile){
MessageBox.Show("String Match, found a string in notepad file");
break;
}
aCounter++;
Console.ReadLine();
}
file.Close();
Upvotes: 0
Views: 261
Reputation: 700382
As the words are sorted, you can use binary search to do the searching part fast:
string[] words = File.ReadAllLines(@"C:\Users\Rider\Documents\Visual Studio 2012\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Resources\enable3.txt");
int index = Array.BinarySearch(words, textBox1.Text);
if (index >= 0) {
MessageBox.Show("String Match, found a string in notepad file");
}
However, this approach is only better if you read the file once, and do multiple searches in the data. If you only do one search, then what you have is as good as any reasonably simple solution can be.
Side note: The file that you have is normally referred to as a text file. The Notepad program is just one of many programs that can edit text files.
Upvotes: 1