如何检查单词与记事本中的大量单词匹配? [英] how to check the word is matching to bulk of words in notepad?

查看:83
本文介绍了如何检查单词与记事本中的大量单词匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经使用过此代码,但搜索大量数据需要花费更多时间。我的记事本中包含10多个缺少的单词。但所有单词都按字母顺序排列。请帮帮我,所以请帮我用字典方式搜索单词。







  int  aCounter =  0 ;  string  aWordInTextFile; 
// 读取文件并逐行显示。
系统。 IO.StreamReader file = new System.IO.StreamReader( @ C:\ Users \ Rider \Documents\Visual Studio 2012 \Projects\WindowsFormsApplication2 \ WindowsFormsApplication2 \ Resources \ enable.txt);
while ((aWordInTextFile = file.ReadLine())!= null
{
Console.WriteLine(aWordInTextFile);
if (textBox1.Text == aWordInTextFile){
MessageBox.Show( 字符串匹配,在记事本文件中找到一个字符串);
break ;
}
aCounter ++;
Console.ReadLine();

}

file.Close();

解决方案

你可以创建哈希集。请参阅 - 在大文本文件中匹配字符串? [ ^ ]


字典用于将键映射到值。如果您只想确定单词列表中是否存在特定单词,则不必使用字典, HashSet< T> [ ^ ]会这样做。请注意,只有在关闭应用程序之前读取一次然后执行多次搜索才有意义,因为显然您必须使用单词列表中的字符串填充HashSet,因此对于单个搜索(在应用程序运行期间)您的当前方法会更快。



1)实例化一个HashSet:var myHashSet = new HashSet< string>();

2)填充它在循环中使用单词列表中的字符串:myHashSet.Add(word);

3)执行查找,如下所示:if(myHashSet.Contains(word))...

already used this code but it takes much more time to search in a bulk of data. my note pad contains more than 10 lack words. but all words are in alphabetical order only so. please help me so please help me to search the words in 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();

解决方案

You can create a HashSet. Refer - Matching a string in a Large text file?[^]


A dictionary is used to map keys to values. If all you want is to determine if a specific word exists in your list of words you don't have to use a dictionary, a HashSet<T>[^] will do. And note that it only makes sense if you read it once and then do multiple searches before closing the application because obviously you have to populate the HashSet with the strings in your list of words, so for a single search (during application run) your current approach would be faster.

1) Instantiate a HashSet: var myHashSet = new HashSet<string>();
2) Populate it in a loop with the strings from the list of words: myHashSet.Add(word);
3) Do the lookup like so: if(myHashSet.Contains(word)) ...


这篇关于如何检查单词与记事本中的大量单词匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆