שתף באמצעות


How to check if a string exists in list Ignoring case sensitivity

Question

Thursday, November 3, 2011 1:24 PM

Hi All,

I want to know if a string exists in the list of array ignoring case sensitivity

I have the following code working for my requirement, but its checking case sensitivity. How can use it ignoring case sensitivity...?

Dim SrtList() As String = {"abc","qwe","zxc"}

Dim chkStr As String = "abc"

If strList.contains(chkStr) Then

  MsgBox ("Item Exists")

Else

  MsgBox ("Item Not Exists")

End If

I want the above code to work even if

Dim chkStr As String = "ABC"

PBL (Visual Studio 2010 Ultimate)

All replies (3)

Thursday, November 3, 2011 1:43 PM ✅Answered

try this:

 

Dim StrList() As String = {"abc", "qwe", "zxc"}

Dim chkStr As String = "ABC"

If Array.Find(StrList, Function(x) x.ToLower = chkStr.tolower) IsNot Nothing Then
    MsgBox("Item Exists")
Else
    MsgBox("Item Not Exists")
End If

thanks for any help


Thursday, November 3, 2011 1:46 PM ✅Answered | 1 vote

On the contains method, you can specify a parameter indicating how you want to compare the strings. You can change the line

If strList.contains(chkStr) Then

To be

If strList.Contains(chkStr, StringComparer.OrdinalIgnoreCase) Then

And it should find the item you are looking for. 


Friday, November 4, 2011 3:36 AM

Paul & soldat,

Both of your suggestions are working.

But soldat, your look more simple and easy approach. I accept that we have n number of approaches.

Thx a lot

PBL (Visual Studio 2010 Ultimate)