Search List(Of String) - Case insensitive

Asked

Viewed 125 times

4

How to look for a String in a List(Of String) without considering the case? I’m using . net 2.0.

Contains does not allow passing any additional parameter. I tried to use Find but it didn’t work either.

Dim v_list As New List(Of String)(New String() {"a", "b", "c"})
Dim v_contains As Boolean
v_contains = v_list.Contains("A")
v_contains = v_list.Find(Function(x) String.Compare(x, "A", True))

3 answers

3

The problems I see in this code:

  • The method Find will return an item from the list, therefore a String. But you declared v_contains like Boolean.
  • The predicate of Find would need to return a boolean value, but it is not what the String.Compare makes (it returns a positive, negative, or zero number).

A possible solution:

Dim v_list As New List(Of String)(New String() {"a", "b", "c"})
Dim v_contains As String
v_contains = v_list.Find(Function(x) x.ToUpper() = "A") ' "a"
  • Wouldn’t it be good to use the Count?

  • 1

    @jbueno Sim, Count<T>() is another possibility. To be honest, I don’t know what is the most recommended solution, I don’t have as much practice with . net.

  • Count<T>() does not work, since it comes from Linq - which was only introduced in . NET 3.5, and AP mentioned that it is using . NET 2.0.

2


No use for Exists?

    Dim v_list As New List(Of String)(New String() {"a", "b", "c"})
    Dim v_contains As Boolean
    v_contains = v_list.Exists(Function(x) String.Compare(x, "A", StringComparison.OrdinalIgnoreCase) = 0)

or if using Find

Dim find = v_list.Find(Function(x) String.Compare(x, "A", StringComparison.OrdinalIgnoreCase) = 0)
  • It should work, but I’m not able to use any of these solutions, I don’t know if it’s because of the version of . NET or some mistake of mine. For now I will use a For Each and when I have more time I check better and choose an answer.

  • 1

    weird... are you sure your version of . net is like 2.0? I just tested here and was

  • It’s really weird. Testing with a new console application (2.0) worked. But in the web application I am working does not compile or list what was the error.

  • It seems that for web does not work the inline function in the argument of the Exists. Using AddressOf worked (example in my reply).

0

The accepted solution was not working for my web application, although it worked normally in a console application. The solution found was to use AddressOf instead of setting the inline function.

Below as was the Default.aspx.Vb:

Imports System.Collections.Generic
Partial Class _Default
    Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim list As New List(Of String)
        Dim exists As Boolean

        list.Add("B")
        list.Add("A")

        exists = list.Exists(AddressOf check)
    End Sub

    Private Shared Function check(ByVal s As String) As Boolean
        Return String.Compare(s, "a", StringComparison.OrdinalIgnoreCase) = 0
    End Function
End Class
  • To pass a string to be searched, you would need to use a member Private class

Browser other questions tagged

You are not signed in. Login or sign up in order to post.