开发者

Search arraylist for keyword

Is there any way to searc开发者_C百科h an arraylist storing a line as a string? I want to search by the first word in every line stored in the arraylist. I tried doing the Arraylist.Contains() and Arraylist.IndexOf but those do not seem to be working? Anyone have any suggestions?

Thanks


string[] matches = arrayList.Cast<string>()
                            .Where(i => i.StartsWith(searchTerm)).ToArray();


Well, you will need to look at each item in the list and decide if the line matches your condition.

So, in pseudo-code:

for each line in listOfLines
    if line StartsWith "some string"
        // It's a match!
    end if
end loop

C#'s String class has a great StartsWith method you could use. How you loop through the list will of course depend on the type of list you have, but as ArrayList appears to implement IList, which implements IEnumerable, then you should have no problem using a foreach(var item in list) type construct to build the loop.


For one, you want to use List<string> (System.Collections.Generic) instead of ArrayList so you can have a strongly-typed list of strings rather than a weakly typed list of objects.

Secondly, no, ArrayList.Contains and ArrayList.IndexOf would perform full object matches, they are not performing partials.

Thirdly, the methodology is going to the same as the answer to your question here. As in, looping over the values contained, and checking each individual string. You could write that to use Linq, but the idea is the same.


Why not just this

var results = from string x in arrayList where x.StartsWith(query);


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ArrayList arraylist =new ArrayList();
        private void Form1_Load(object sender, EventArgs e)
        {
            arraylist.Add("This is line1");
            arraylist.Add("Jack is good boy");
            arraylist.Add("Azak is a child");
            arraylist.Add("Mary has a chocolate");
            MessageBox.Show(GetSentenceFromFirstChar('J'));

        }
        public string GetSentenceFromFirstChar(char firstcharacter)
        {
            bool find=false;
            int index=-1;
            for (int i = 0; i < arraylist.Count; i++)
            {
                if ((char)arraylist[i].ToString()[0] == firstcharacter)
                {
                    index = i;
                    find = true;
                    break;
                }
            }
            if (find)
            {
                return arraylist[index].ToString();
            }
            else
            {
                return "not found";
            }
        }


    }
}

maybe helps..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜