开发者

c# searching arraylist

I am making a simple app that reads data in from a file. So far i have been able to read in all the da开发者_如何学编程ta into an arraylist.

However i need to give the user the ability to search the arraylist and return all the values associated with their search. To search the user enters a keyword or what ever into a text box and when they click search the related results will appear in a list box.

What code do i need to be able to search an arraylist.


Perhaps you want to do something like this:

Loading the file into a List<string>:

List<string> lines=File.ReadAllLines(filename);

Searching in the List<string>:

IEnumerable<string> foundLine=lines.Where(s=>s.Contains(searchString));
foreach(string foundLine in lines)
  listBox1.Items.Add(foundLine);

Note that string.Contains uses ordinal comparison(case sensitive, culture invariant), that might not be what you want. And it doesn't deal with non normalized unicode sequences either.

You could use the following extension method to support other comparision modes:

public static bool Contains(this string str, string value, StringComparison comparisonType)
{
  return str.IndexOf(value, comparisonType) >= 0;
}

https://connect.microsoft.com/VisualStudio/feedback/details/435324/the-string-contains-method-should-include-a-signature-accepting-a-systen-stringcomparison-value#


You can use:

string searchString = txtSearch.Text.Trim();
ArrayList arrayResult = new ArrayList();
foreach(object obj in arrayList)
{
   if(searchString == Convert.ToString(obj))
   {
      arrayResult.Add(obj);
   }

}
ListBox.DataSource = arrayResult;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜