Usher
Usher

Reputation: 2146

what's wrong with string contains match

Sorry, I don't want to post this question here. Other questions like mine have been posted before, but I haven't found an answer yet.

I am trying to do a simple string match using string.contains

 If Not dataRow("Name").ToString().Contains(UserName) Then
  --do something--
End If

but that's not working in my condition

dataRow("Name").ToString()= "Steve Cook"
UserName="Cook Steve"

I want to find the %like match (like SQL) but .contains not what I was expecting it to do?

Upvotes: 0

Views: 213

Answers (3)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112602

You can solve the problem by using Regex. Regex is comparable to using wildcards but allows you to express much more complex search patterns. Google for Regex tutorials. You will find lots of them.

Dim s As String = dataRow("Name").ToString()
If Not Regex.IsMatch(s, "\bSteve\s+Cook\b|\bCook\s+Steve\b") Then
    ' do something
End If

Requires

Imports System.Text.RegularExpressions

The meaning of this Regex expression is: find the whole words (i.e. not only parts of words imbedded in a longer word) "Steve Cook" OR "Cook Steve" that are separated by at least one space character.

Upvotes: 2

Jimi
Jimi

Reputation: 32278

A simple Comparer Class.
If the strings differ in length or not all parts are equal, returns false

Dim names As Names = New Names
names.CompareName = "Steve Cook"
names.ToName = "Cook steve"

If names.Compare(True) = True Then
 Console.Write("Same guy")
End If

Public Class Names
  Public Property CompareName() As String
  Public Property ToName() As String

  Public Function Compare(ByVal IgnoreCase As Boolean) As Boolean
     If CompareName.Length <> ToName.Length Then Return False

     Dim TypeOfComparison As StringComparison = 
         If(IgnoreCase, StringComparison.InvariantCultureIgnoreCase, 
                        StringComparison.InvariantCulture)
     Dim _All As Boolean = True
     Dim _parts As String() = ToName.Split(" "c)
     For Each _part As String In _parts
        If CompareName.IndexOf(_part, 0, TypeOfComparison) < 0 Then
           _All = False
           Exit For
        End If
     Next
     Return _All
  End Function
End Class

Or with a Join-Split-Reverse:

Dim Name1 As String = "Steve Cook"
Dim Name2 As String = "Cook Steve"

If StringCompare(Name1, Name2) Then
    Console.Write("Same guy")
End If

Public Function StringCompare(ByVal CompName As String, ByVal ToName As String) As Boolean
   If CompName.Equals(ToName, StringComparison.InvariantCultureIgnoreCase) Then
       Return True
   End If
   If CompName.Equals(String.Join(" ", ToName.Split(" "c).AsEnumerable.Reverse()),
                      StringComparison.InvariantCultureIgnoreCase) Then
      Return True
   End If
   Return False
End Function

Upvotes: 2

Tim Hall
Tim Hall

Reputation: 1450

It has to be an exact part of the string, in your case the field does not contain “Cook Steve” like you’re asking, it contains (“Steve” and “Cook”) or (“Steve Cook”)

You need to split on the spaces and check each word. But you could find some cases where you’ll get false positives.

Upvotes: 2

Related Questions