从文本文件中搜索项目 [英] Search items from textfile
问题描述
假设我在C上有一个文本文件://file.txt
在该文本文件中包含 -
CodeProject
Coogle
代码
Cripsy
Cyclone
Crime
罪犯
狗
Din
完成
做
值班
桌子
鹰
耳朵
吃
.. .....
等
现在我的问题是 -
我有一个csharp winform应用程序
Button1
Textbox1
Listbox1
我想在按钮1上放一个代码 -
如果我在textbox1中写= E
在列表框中显示所有Word都是以file.txt中的E开头
如果我输入Do
然后显示
完成
做
狗
怎么做?
感谢高级
Suppose I have a textfile on C://file.txt
On that text file contains -
CodeProject
Coogle
Code
Cripsy
Cyclone
Crime
Criminal
Dog
Din
Done
Doing
Duty
Desk
Eagle
Ear
Eat
.......
etc
Now my question is -
I have a csharp winform application
Button1
Textbox1
Listbox1
I wanna put a code on button1-
If I write in textbox1= E
On listbox show all Word started with E from file.txt
If I type "Do"
then show
Done
Doing
Dog
How to do this?
Thanks in advanced
推荐答案
框架轻轻提供String.StartsWith方法 [ ^ ]。
在一个天真的方法中,你逐行阅读文件并使用建议的方法,如果匹配,那么你将读取的单词添加到列表框。
在更聪明的方法中你可以将整个文件读入内存(例如,一组字符串)然后搜索到这样的缓存信息。
The framework gently provides the String.StartsWith method[^].
In a naive approach you read line by line the file and use the suggested method, if there is match then you add the read word to the list box.
In amore smart approach you could read the whole file once into memory (e.g. in a collection of strings) and then search into such cached info.
读取文本文件的项目并在列表< string>()$中添加这些项目c $ c>。对于其他工作,请遵循以下步骤:
保持自动完成列表打开 [ ^ ]
Read the items of textfile and add those items in aList<string>()
.For rest of the work follow this one:
Keep AutoComplete List Open[^]
protected void Button_Click(object sender, EventArgs e)
{
string fileLoc = Server.MapPath("You Location");
if (File.Exists(fileLoc))
{
String line;
using (TextReader tr = new StreamReader(fileLoc))
{
line = tr.ReadToEnd();
}
string[] read=line.StartsWith(textboxName.text);
for(int i=0;i<read.length;i++)
{
//your logic;
}
}
}
这篇关于从文本文件中搜索项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!