Results 1 to 7 of 7

Thread: [RESOLVED] Compare string with items in List of String, ignoring case-sensitive ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Resolved [RESOLVED] Compare string with items in List of String, ignoring case-sensitive ?

    Hi,

    I have a string which user writes in app. Now I want to compare this string with items in List of strings, but without case-sensitive.

    This is what I have so far:

    Code:
      Dim ListToCheck As String() = {"London", "Paris", "Amsterdam"}
    
      Dim MyString = "John is currently at a meeting in london."
    
      Dim b As Boolean = ListToCheck.Any(Function(s) MyString.Contains(s))
    
                    If b = True Then
    
                        MsgBox("string contains an item from List")
                    Else
    
                        MsgBox("string doesn't contain an item from List")
    
                    End If
    This returns correct result, but case-sensitive. I know that "StringComparer.CurrentCultureIgnoreCase" does that thing, but can I include It into LINQ, and how ?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compare string with items in List of String, ignoring case-sensitive ?

    Your issue there is that the String.Contains method does not support case-insensitive comparisons for some reason. What you can do is use String.IndexOf instead, which does have overloads that can be used to ignore case. Note that IndexOf returns an index rather a Boolean, so you must compare that index to -1 to get your Boolean value. -1 means that the original does not contain the substring and anything else means that it does.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compare string with items in List of String, ignoring case-sensitive ?

    It's actually quite easy to write your own Contains method that does support case-insensitive comparisons. You can add this module to your project to provide such a method that extends the String type:
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. Friend Module StringExtensions
    4.  
    5.     <Extension>
    6.     Public Function Contains(source As String,
    7.                              value As String,
    8.                              comparisonType As StringComparison) As Boolean
    9.         Return source.IndexOf(value, comparisonType) <> -1
    10.     End Function
    11.  
    12. End Module
    Because it's an extension method, you can call it as you would a conventional module member:
    vb.net Code:
    1. Dim result = Contains(myString, mySubstring, myStringComparison)
    or you can call it with two arguments as though it's a member of the what would otherwise be the first argument:
    vb.net Code:
    1. Dim result = myString.Contains(mySubstring, myStringComparison)
    That means that you can call that method in place of the String.Contains methodthat you're already calling simply by adding the second argument.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Compare string with items in List of String, ignoring case-sensitive ?

    Thanks jmcilhinney,

    but how would a 1 line code using IndexOf look like, Is It even possible ?

    My solution is also this:

    Code:
     Dim ListToCheck As String() = {"LONDON", "PARIS", "AMSTERDAM"}
    
    Dim MyString = "John is currently at a meeting in london."
    
      Dim b As Boolean = ListToCheck.Any(Function(s) MyString.ToUpper.Contains(s))
    
                    If b = True Then
    
                        MsgBox("string contains an item from List")
                    Else
    
                        MsgBox("string doesn't contain an item from List")
    
                    End If
    Basically, I wrote all string in a List with upper case, and compare that with MyString.ToUpper. I'm not sure what is better, and most important - faster.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compare string with items in List of String, ignoring case-sensitive ?

    It's generally not a good idea to try to implement a case-insensitive comparison by comparing all upper-case or all lower-case. It will work sometimes but not others.

    As for using IndexOf, I already told you what to do. Instead of calling Contains to get a Boolean value, you call IndexOf and compare the result to -1 to get a Boolean. You just have to read the code in the extension method I provided to see how to implement the equivalent of Contains using IndexOf.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Compare string with items in List of String, ignoring case-sensitive ?

    Ok,

    thanks a lot.

  7. #7
    Member
    Join Date
    Sep 2013
    Posts
    59

    Re: [RESOLVED] Compare string with items in List of String, ignoring case-sensitive ?

    I was looking for exactly THIS! Thanks jmcilhinney .... Brilliant!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width