Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

comparing matchcase strings

i have string one and i have string two. I want to compare the 2 strings. If string one has * in it ,then it will be considered to be a string like below

eg ca*   = car,care, cat   etc

how can this be done?
0
abababa
Asked:
abababa
  • 4
  • 3
  • 3
  • +3
1 Solution
 
armoghanCommented:

Why dont you use StartsWith function ?
Dim sms1 As String = "care"

If sms2.StartsWith("car") = True Then
Console.WriteLine("yes")
        End If
0
 
armoghanCommented:
and use substring to get the ca before *
Dim sms2 As String = "ca*"
sms2.Substring (0, sms2.IndexOf ("*"))

This will return "ca"
0
 
armoghanCommented:
Dim sms1 As String = "ca*"
Dim sms2 As String = "care"

If sms2.StartsWith(sms1.Substring (0, sms1.IndexOf ("*"))) = True Then
Console.WriteLine("yes")
End If
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
wguerramCommented:
       Dim str As String = "ca*"
        Dim str2 As String = "care"

        'If the last char is an * then compare the first chars        
        If Right(str, 1) = "*" Then
            'If the string matched was found starting from the first char return true otherwise false
            Debug.WriteLine(IIf(InStr(str2, Left(str, 1)) = 1, True, False))
        Else
            Debug.WriteLine(str = str2)
        End If
0
 
tzxie2000Commented:
test code below
and you can found the index of first index of match string

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim s As String = "ca*"
    Dim searchs As String = "wcare"

    Dim reg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("ca*")
    Dim m As System.Text.RegularExpressions.Match = reg.Match(searchs)
    If (m.Success) Then
        MessageBox.Show(m.Index.ToString())
    End If
End Sub
0
 
abababaAuthor Commented:
sorry but i dont know if any of those do what word does

if someone puts c*r*

this could mean carmen,care,curry  , i dont think any of the above answers do this
0
 
tzxie2000Commented:
I test the input you write,its ok
0
 
webwebwebwebCommented:
 so i used ur code and got  this.  I change the searchs string to carmen,care and it brings up a message box saying its matched but wen i put in cd it still brings up that it matches but it should be that it doesnt match, what is not right here?


 Dim s As String = "ca*"
 Dim searchs As String = "wcare"

 Dim reg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("ca*")
        Dim m As System.Text.RegularExpressions.Match = reg.Match(searchs)
        If (m.Success) Then
            MsgBox("match")
        Else
            MsgBox("no match")
        End If
0
 
tzxie2000Commented:
sorry for a little mistake

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim s As String = "ca*"
    Dim searchs As String = "wcare"

    Dim reg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(s)'<-this should be s not "ca*" as we want to change the search pattern
    Dim m As System.Text.RegularExpressions.Match = reg.Match(searchs)
    If (m.Success) Then
        MessageBox.Show("I found at "+m.Index.ToString())
    else
        MessageBox.Show("sorry not found")
    End If
End Sub
0
 
webwebwebwebCommented:
i put in the following and it brings back that it matches, c shouldnt match but it does and i tried putting d in searchs and it says no match so thats working fine. It seems if its got a c then it matches

Dim s As String = "ca*"
Dim searchs As String = "c"

Dim reg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(s) '<-this should be s not "ca*" as we want to change the search pattern
        Dim m As System.Text.RegularExpressions.Match = reg.Match(searchs)
        If (m.Success) Then
            MsgBox("match")
        Else
            MsgBox("no match")
        End If
0
 
abababaAuthor Commented:
yeh thats what happens
0
 
armoghanCommented:
for maching such strings c*r*
You need to use Regular Expressions..
For simple things like c* you can use the code i mentioend above
0
 
eozz_2000Commented:
I did this function, try with it:

    Private Function Matches(ByVal strPattern As String, ByVal strString As String) As Boolean
        If strPattern.IndexOf("*") = 0 Then
            If strPattern.Length = 1 Then
                Return True
            Else
                If strPattern.Substring(1, strPattern.Length - 1).IndexOf("*") <> -1 Then
                    Dim strBetweenAsterisks As String
                    strBetweenAsterisks = strPattern.Substring(1, strPattern.Substring(1, strPattern.Length - 1).IndexOf("*"))
                    If strString.IndexOf(strBetweenAsterisks) <> -1 Then
                        Return Me.Matches(strPattern.Substring(1, strPattern.Length - 1), strString.Substring(strString.IndexOf(strBetweenAsterisks), strString.Length - strString.IndexOf(strBetweenAsterisks)))
                    Else
                        Return False
                    End If
                Else
                    Dim strAfterAsterisk As String
                    strAfterAsterisk = strPattern.Substring(1, strPattern.Length - 1)
                    If strString.IndexOf(strAfterAsterisk) <> -1 And strString.IndexOf(strAfterAsterisk) + strAfterAsterisk.Length = strString.Length Then
                        Return True
                    Else
                        Return False
                    End If
                End If
            End If
        Else
            If strPattern.Substring(1, strPattern.Length - 1).IndexOf("*") <> -1 Then
                If strString.StartsWith(strPattern.Substring(0, strPattern.IndexOf("*"))) Then
                    Dim strFromAsterisk As String
                    strFromAsterisk = strPattern.Substring(strPattern.IndexOf("*"), strPattern.Length - strPattern.IndexOf("*"))
                    Return Me.Matches(strFromAsterisk, strString.Substring(strPattern.Substring(0, strPattern.IndexOf("*")).Length, strString.Length - strPattern.Substring(0, strPattern.IndexOf("*")).Length))
                Else
                    Return False
                End If
            Else
                If strPattern = strString Then
                    Return True
                Else
                    Return False
                End If
            End If
        End If
    End Function

You have only to validate the strPattern <> ""
0
 
eozz_2000Commented:
Hey man, I have already fixed your problem with this function... WHERE ARE YOU?
0
 
eozz_2000Commented:
Well... I have solved the problem... where are my points?, is it valid to ask and leave the question that has already an answer?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now