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
Here's an example of how you could do it:
Dim myListAs New System.Collections.Generic.List(OfString)()myList.Add("Apple")myList.Add("Banana")myList.Add("Pear")myList.Add("Apricot")Dim textAs String ="Ap"Dim myFilteredListAs New System.Collections.Generic.List(OfString)For Each itemAs String In myListIf item.StartsWith(text)ThenmyFilteredList.Add(item)End IfNextI was looking to use the FindAll method of the Generic.List and a Predicate.
Does anyone knows who to do this having the parameter, which is in this case, the prefix?
Thanks,
Miguel
Imports System.Collections.GenericPartialClass MyPosts_GeneralASPNet_GettingStarted_1168114Inherits System.Web.UI.PagePrivate prefixAs String ="d"Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.LoadDim dinosaursAs New List(OfString)dinosaurs.Add("Compsognathus")dinosaurs.Add("Amargasaurus")dinosaurs.Add("Oviraptor")dinosaurs.Add("Velociraptor")dinosaurs.Add("Deinonychus")dinosaurs.Add("Dilophosaurus")dinosaurs.Add("Gallimimus")dinosaurs.Add("Triceratops")Dim subListAs List(OfString) = dinosaurs.FindAll(AddressOf BeginsWithPrefix)For Each dinosaurAs String In subListResponse.Write(dinosaur &"<br />")NextEnd SubPrivate Function BeginsWithPrefix(ByVal sAs String)As BooleanReturn s.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)End FunctionEnd Class
0 comments:
Post a Comment