Can we create a case insensitive List of Strings in Vb.net?

ipjyo
ipjyo used Ask the Experts™
on
Hi,
Could you help me with how to create case insensitive List(Of String) in Vb.Net?
My code is as follows.

Private fieldNames As List(Of String)
Using fileReader As New FileIO.TextFieldParser(filepath)
                    fileReader.TextFieldType = FileIO.FieldType.Delimited
                    fileReader.SetDelimiters(",")
 
                    If fileReader.EndOfData Then
                        Throw New Exception("There was no data in" & filepath & "file")
                    Else
                        fieldNames = New List(Of String)(fileReader.ReadFields)
                    End If
 End Using
 
Now I am trying to call the below function as 
dim isPresent as String = GetValue(TextBox1.Text.ToString())
 
Function GetValue(ByVal field As String) As String
            Dim index As Integer = fieldNames.IndexOf(field)
          if index >0 then return "found"
         else
           return "not found"
         end if
End Function
 
It is doing case sensitive search. But I want this to do case insensitive.
Could anybody help me with this.
Thank you in advance.

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
After you load your list, convert it all to lower case:

                fieldNames = New List(Of String)(fileReader.ReadFields)
                For i As Integer = 0 To fieldNames.Count - 1
                    fieldNames(i) = fieldNames(i).ToLower
                Next

Then convert the value being searched for to lower case as well:

    Function GetValue(ByVal field As String) As String
          Dim index As Integer = fieldNames.IndexOf(field.ToLower) ' <---- field to lowercase as well
          if index >0 then return "found"
         else
           return "not found"
         end if
    End Function
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
*IndexOf() returns -1 if not found so you would actually need:

    If index >= 0 Then

Or:

    If index <> -1 Then

But I would just use this instead:

    Function GetValue(ByVal field As String) As String
        Return IIf(fieldNames.Contains(field.ToLower), "found", "not found")
    End Function

Author

Commented:
Hi,
Thank you very much. I will try now and let you know very soon.

Author

Commented:
It is working exactly as what I wanted.
Thanks again!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial