List(Of String)

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

How can I filter a List(Of String)?
I need to get the list elements which start with the letters contained
in the variable Text.

Thanks,
Miguel
 
shapper said:
Hello,

How can I filter a List(Of String)?
I need to get the list elements which start with the letters contained
in the variable Text.

Thanks,
Miguel

You loop through the items in the list and check the values. There are a
lot of fancy ways of doing it, like using the FindAll method or creating
a filtering enumerator, but they all boil down to looping through all
the items.

If the list is sorted, you can use the BinarySearch method to quickly
locate the items in the list, so that you don't have to loop through the
entire list.
 
Here's both the .net 2.0 example using the predicate delegate and the .net
3.0 example using extension methods.

List<string> myListOfStrings = new List<string>();

myListOfStrings.Add("Hello");
myListOfStrings.Add("World");
myListOfStrings.Add("Coding");
myListOfStrings.Add("C#");
myListOfStrings.Add("Control");


// ASP.NET 3.5 - Extension Method
foreach (string word in myListOfStrings.Where(i => i.StartsWith("C")))
{
Response.Write(word + "<br/>");
}

Response.Write("<br/>");

// ASP.NET 2.0 - Using the Predicate delegate
foreach (string word in myListOfStrings.FindAll(delegate(string
thisWord) { return thisWord.StartsWith("C") ? true : false; }))
{
Response.Write(word + "<br/>");
}


Output:

Coding
C#
Control

Coding
C#
Control

HTH.

-dl
 
Whoops, just realized you had a VB example. Here's the translation, though...
with VB, you're not gaining a lot with the Extension method.

Dim myListOfStrings As New List(Of String)()

myListOfStrings.Add("Hello")
myListOfStrings.Add("World")
myListOfStrings.Add("Coding")
myListOfStrings.Add("C#")
myListOfStrings.Add("Control")

' ASP.NET 3.5 - Extension Method

For Each word In myListOfStrings.Where(Function(thisWord) thisWord.StartsWith("C"))
Response.Write(word + "<br/>")
Next

Response.Write("<br/>")

' ASP.NET 2.0 - Using the Predicate delegate
For Each word In myListOfStrings.FindAll(Function(thisWord) thisWord.StartsWith("C"))
Response.Write(word + "<br/>")
Next

-dl
 
Here's both the .net 2.0 example using the predicate delegate and the .net
3.0 example using extension methods.

List<string> myListOfStrings = new List<string>();

myListOfStrings.Add("Hello");
myListOfStrings.Add("World");
myListOfStrings.Add("Coding");
myListOfStrings.Add("C#");
myListOfStrings.Add("Control");

// ASP.NET 3.5 - Extension Method
foreach (string word in myListOfStrings.Where(i => i.StartsWith("C")))
{
Response.Write(word + "<br/>");
}

Response.Write("<br/>");

// ASP.NET 2.0 - Using the Predicate delegate
foreach (string word in myListOfStrings.FindAll(delegate(string
thisWord) { return thisWord.StartsWith("C") ? true : false; }))
{
Response.Write(word + "<br/>");
}

Output:

Coding
C#
Control

Coding
C#
Control

HTH.

-dl

Hi,

I am having a few problems with your code. I use VB.NET and somehow I
am not being able to convert it.

I need to use predicated in .NET 2.0 but I also need to specify which
letter to start with:
return thisWord.StartsWith(MyFirstLetter)

Anyway, I tried to make this work in VB.NET but no lucky. Could
someone help me out?

This is really strange because I always convert between C# and VB.NET.

Thanks,
Miguel
 
I thought I corrected and posted some VB examples too... maybe they haven't
hit the servers yet.

Here it is in VB. I extracted it out into a variable... give this a try:

Dim firstLetter As String
firstLetter = "C"
Dim myListOfStrings As New List(Of String)()

myListOfStrings.Add("Hello")
myListOfStrings.Add("World")
myListOfStrings.Add("Coding")
myListOfStrings.Add("C#")
myListOfStrings.Add("Control")

' ASP.NET 2.0 - Using the Predicate delegate
For Each word In myListOfStrings.FindAll(Function(thisWord) thisWord.StartsWith(firstLetter))
Response.Write(word + "<br/>")
Next

HTH.

-dl
 
I thought I corrected and posted some VB examples too... maybe they haven't
hit the servers yet.

Here it is in VB. I extracted it out into a variable... give this a try:

Dim firstLetter As String
firstLetter = "C"
Dim myListOfStrings As New List(Of String)()

myListOfStrings.Add("Hello")
myListOfStrings.Add("World")
myListOfStrings.Add("Coding")
myListOfStrings.Add("C#")
myListOfStrings.Add("Control")

' ASP.NET 2.0 - Using the Predicate delegate
For Each word In myListOfStrings.FindAll(Function(thisWord) thisWord.StartsWith(firstLetter))
Response.Write(word + "<br/>")
Next

HTH.

-dl

Hi,

I am getting an error in:
For Each word In myListOfStrings.FindAll(Function(thisWord)
thisWord.StartsWith(firstLetter))

on word Function. It says expression expected. I don't know why.

I was trying something like:
categories.FindAll(AddressOf FindAllByPrefix)

Where FindAllByPrefix is a function that checks if the word starts
with that prefix.
The problem is that in this case I am not able to pass the PrefixValue
to the function.

I was following this example:
http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx

Any idea?

Of course I would use a simple loop but I am trying to make this work
with Predicate.

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VBA: How can I use a string variable as autofilter criteria? 4
Enum to Array Of String 1
Generic List to String 4
Generic List 3
Get Substrings 2
Unit.Pixel to String 1
Remove last letter from string 2
String 2

Back
Top