tags:

views:

98

answers:

7

Hello,

I have a list like this:

item.Add("a");
item.Add("as");
item.Add("b");
item.Add("fgs");
item.Add("adsd");

How can I find all items that start with (for example) "a"?

This "a" is not some hardcoded string, so I will need a function that do this for each string.

I try with FindAll, but I did not figured out how it works.

Br, Wolfy

A: 

Try this

item.FindAll(i => i.Contains("a"));

This will return a List containting only the filtered strings.

Øyvind Bråthen
Shouldn't that be `StartsWith` instead of `Contains`?
Brian Rasmussen
@Brian: Should have been StartsWith. I read his question a bit to quick.
Øyvind Bråthen
A: 
var selectedList = from s in item where s.StartsWith("a") select s;
MAKKAM
After that selectedList (IEnumerable) points on the elements of item starting with "a";
MAKKAM
+2  A: 

Or with LINQ

from i in items where i.StartsWith("a") select i;
Noffls
+1  A: 

for NET2.0 you may use this method: 'pattern' is an argument to look for (f.e. "a")

    private List<string> FindAll(List<string> list, string pattern)
    {       // returns found results
            return list.FindAll(delegate(string item)
                            {
                                return item.StartsWith(pattern);

                            });
    }
Arseny
+1  A: 

I thought you have another list that contains the startswith criteria strings. Lets call your items "words" and the other list "keywords". So the below query will return what you want.

List<string> words = new List<string>();
words.Add("a");
words.Add("as");
words.Add("b");
words.Add("fgs");
words.Add("adsd");

List<string> keywords = new List<string>();
keywords.Add("a");
keywords.Add("b");

var result = words.FindAll(o =>
    keywords.Any(a => o.StartsWith(a))
);

This result has the words that starts with any of the keyword from keywords.

Musa Hafalır
+1 I think this is what OP want. And a suggestion: change the variable name from "list" to "keywords" and change "item" to "words".
Danny Chen
Thanks Danny Chen. I have edited my answer based on your suggestion.
Musa Hafalır
+7  A: 

If by "start with" you mean the first char, then:

item.FindAll(i => i[0] == 'a');

if you mean a string (may be other than 1 char) then:

item.FindAll(i => i.StartsWith("a"));

If you want a different comparison, such as case-insensitive, locale-based, etc. then do the relevant IndexOf such as:

item.FindAll(i => i.IndexOf("a", StringComparison.CurrentCultureIgnoreCase) == 0);

All of the above can be easily adapted to be use a relevant char or string variable or parameter.

If you don't need the extra properties and methods provided by a list, then it will be more efficient to use Where than FindAll as FindAll creates a new list, and does so in all at once, while Where will enumerate the matching results as it is iterated through.

Jon Hanna
+1  A: 
List<string> item = new List<string>();
            item.Add("a");
            item.Add("as");
            item.Add("b");
            item.Add("fgs");
            item.Add("adsd");

            var res1 = item.FindAll(i => i.StartsWith("a"));
            var res2 = item.Where(i => i.StartsWith("a"));
priyanka.sarkar_2